<!DOCTYPE html>



  


<html class="theme-next mist use-motion" lang="zh-CN">
<head><meta name="generator" content="Hexo 3.8.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css">







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css">

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="Hexo, NexT">










<meta name="description" content="目录">
<meta property="og:type" content="article">
<meta property="og:title" content="数据库之MySQL基础">
<meta property="og:url" content="http://luqilinok.github.io/2019/07/17/mysql/index.html">
<meta property="og:site_name" content="Lu QiLin&#39;s Blog">
<meta property="og:description" content="目录">
<meta property="og:locale" content="zh-CN">
<meta property="og:image" content="https://img-blog.csdn.net/20170505201016682">
<meta property="og:updated_time" content="2019-07-18T11:59:02.401Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="数据库之MySQL基础">
<meta name="twitter:description" content="目录">
<meta name="twitter:image" content="https://img-blog.csdn.net/20170505201016682">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Mist',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: 'Author'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://luqilinok.github.io/2019/07/17/mysql/">





  <title>数据库之MySQL基础 | Lu QiLin's Blog</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-CN">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Lu QiLin's Blog</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br>
            
            Home
          </a>
        </li>
      
        
        <li class="menu-item menu-item-technology">
          <a href="/categories/Technology/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br>
            
            Technology
          </a>
        </li>
      
        
        <li class="menu-item menu-item-notes">
          <a href="/categories/Notes/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br>
            
            Notes
          </a>
        </li>
      
        
        <li class="menu-item menu-item-personal">
          <a href="/categories/Personal/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br>
            
            Personal
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br>
            
            About
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://luqilinok.github.io/2019/07/17/mysql/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="LuQiLin">
      <meta itemprop="description" content>
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Lu QiLin's Blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">数据库之MySQL基础</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">Posted on</span>
              
              <time title="Post created" itemprop="dateCreated datePublished" datetime="2019-07-17T14:23:30+08:00">
                2019-07-17
              </time>
            

            

            
          </span>
          
  <span class="post-updated">
    &nbsp; | &nbsp; 更新于
    <time itemprop="dateUpdated" datetime="2019-07-18T19:59:02+08:00" content="2019-07-18">
      2019-07-18
    </time>
  </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">In</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/Notes/" itemprop="url" rel="index">
                    <span itemprop="name">Notes</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>目录</p>
<a id="more"></a>
<p>=================</p>
<ul>
<li><a href="#1sql%E8%AF%AD%E8%A8%80%E4%B8%BB%E8%A6%81%E5%88%86%E4%B8%BA%E4%B8%89%E7%B1%BB">1、SQL语言主要分为三类</a></li>
<li><a href="#2%E5%BA%93%E6%93%8D%E4%BD%9C">2、库操作</a><ul>
<li><a href="#21-%E6%96%B0%E5%A2%9E%E6%95%B0%E6%8D%AE%E5%BA%93">2.1 新增数据库</a></li>
<li><a href="#22-%E6%9F%A5%E8%AF%A2%E6%95%B0%E6%8D%AE%E5%BA%93">2.2 查询数据库</a></li>
<li><a href="#23-%E6%9B%B4%E6%96%B0%E6%95%B0%E6%8D%AE%E5%BA%93">2.3 更新数据库</a></li>
<li><a href="#24-%E5%88%A0%E9%99%A4%E6%95%B0%E6%8D%AE%E5%BA%93">2.4 删除数据库</a></li>
</ul>
</li>
<li><a href="#3%E8%A1%A8%E6%93%8D%E4%BD%9C">3、表操作</a><ul>
<li><a href="#31-%E6%96%B0%E5%A2%9E%E8%A1%A8">3.1 新增表</a></li>
<li><a href="#32-%E6%9F%A5%E8%AF%A2%E8%A1%A8">3.2 查询表</a></li>
<li><a href="#33-%E6%9B%B4%E6%96%B0%E8%A1%A8">3.3 更新表</a></li>
<li><a href="#34-%E5%88%A0%E9%99%A4%E8%A1%A8">3.4 删除表</a></li>
</ul>
</li>
<li><a href="#4%E6%95%B0%E6%8D%AE%E6%93%8D%E4%BD%9C">4、数据操作</a><ul>
<li><a href="#41-%E6%96%B0%E5%A2%9E%E6%95%B0%E6%8D%AE">4.1 新增数据</a></li>
<li><a href="#42-%E6%9F%A5%E8%AF%A2%E6%95%B0%E6%8D%AE">4.2 查询数据</a></li>
<li><a href="#43-%E6%9B%B4%E6%96%B0%E6%95%B0%E6%8D%AE">4.3 更新数据</a></li>
<li><a href="#43-%E5%88%A0%E9%99%A4%E6%95%B0%E6%8D%AE">4.3 删除数据</a></li>
</ul>
</li>
<li><a href="#5%E5%AD%97%E7%AC%A6%E9%9B%86%E9%97%AE%E9%A2%98">5、字符集问题</a></li>
<li><a href="#6%E6%A0%A1%E5%AF%B9%E9%9B%86%E9%97%AE%E9%A2%98">6、校对集问题</a></li>
<li><a href="#7%E6%95%B0%E5%80%BC%E5%9E%8B">7、数值型</a><ul>
<li><a href="#71-%E6%95%B4%E6%95%B0%E5%9E%8B">7.1 整数型</a></li>
<li><a href="#72-%E5%B0%8F%E6%95%B0%E5%9E%8B">7.2 小数型</a></li>
</ul>
</li>
<li><a href="#8%E6%97%A5%E6%9C%9F%E6%97%B6%E9%97%B4%E7%B1%BB%E5%9E%8B">8、日期时间类型</a></li>
<li><a href="#9%E5%AD%97%E7%AC%A6%E7%B1%BB%E5%9E%8B">9、字符类型</a></li>
<li><a href="#10%E8%AE%B0%E5%BD%95%E9%95%BF%E5%BA%A6">10、记录长度</a></li>
<li><a href="#11%E5%88%97%E5%B1%9E%E6%80%A7">11、列属性</a><ul>
<li><a href="#111-%E7%A9%BA%E5%B1%9E%E6%80%A7">11.1 空属性</a></li>
<li><a href="#112-%E5%88%97%E6%8F%8F%E8%BF%B0">11.2 列描述</a></li>
<li><a href="#113-%E9%BB%98%E8%AE%A4%E5%80%BC">11.3 默认值</a></li>
<li><a href="#114-%E4%B8%BB%E9%94%AE">11.4 主键</a></li>
<li><a href="#115-%E8%87%AA%E5%8A%A8%E5%A2%9E%E9%95%BF">11.5 自动增长</a></li>
<li><a href="#116-%E5%94%AF%E4%B8%80%E9%94%AE">11.6 唯一键</a></li>
</ul>
</li>
<li><a href="#12%E7%B4%A2%E5%BC%95">12、索引</a></li>
<li><a href="#13%E5%85%B3%E7%B3%BB">13、关系</a><ul>
<li><a href="#131-%E4%B8%80%E5%AF%B9%E4%B8%80">13.1 一对一</a></li>
<li><a href="#132--%E4%B8%80%E5%AF%B9%E5%A4%9A">13.2  一对多</a></li>
<li><a href="#133-%E5%A4%9A%E5%AF%B9%E5%A4%9A">13.3 多对多</a></li>
</ul>
</li>
<li><a href="#14%E8%8C%83%E5%BC%8F">14、范式</a><ul>
<li><a href="#141-1nf">14.1 1NF</a></li>
<li><a href="#142-2nf">14.2 2NF</a></li>
<li><a href="#143-3nf">14.3 3NF</a></li>
<li><a href="#144-%E9%80%86%E8%A7%84%E8%8C%83%E5%8C%96">14.4 逆规范化</a></li>
</ul>
</li>
<li><a href="#15%E4%B8%BB%E9%94%AE%E5%86%B2%E7%AA%81">15、主键冲突</a></li>
<li><a href="#16%E8%A0%95%E8%99%AB%E5%A4%8D%E5%88%B6">16、蠕虫复制</a></li>
<li><a href="#17%E6%95%B0%E6%8D%AE%E5%BA%93%E9%AB%98%E7%BA%A7%E6%93%8D%E4%BD%9C">17、数据库高级操作</a><ul>
<li><a href="#171-%E6%9B%B4%E6%96%B0%E6%95%B0%E6%8D%AE">17.1 更新数据</a></li>
<li><a href="#172-%E5%88%A0%E9%99%A4%E6%95%B0%E6%8D%AE">17.2 删除数据</a></li>
</ul>
</li>
<li><a href="#18%E6%9F%A5%E8%AF%A2%E9%AB%98%E7%BA%A7%E6%93%8D%E4%BD%9C">18、查询高级操作</a><ul>
<li><a href="#181-select-%E9%80%89%E9%A1%B9">18.1 select 选项</a></li>
<li><a href="#182-%E5%AD%97%E6%AE%B5%E5%88%AB%E5%90%8D">18.2 字段别名</a></li>
<li><a href="#183-%E6%95%B0%E6%8D%AE%E6%BA%90">18.3 数据源</a></li>
<li><a href="#184-where%E5%AD%90%E5%8F%A5">18.4 where子句</a></li>
<li><a href="#185-group-by-%E5%AD%90%E5%8F%A5">18.5 group by 子句</a></li>
<li><a href="#186-having-%E5%AD%90%E5%8F%A5">18.6 having 子句</a></li>
<li><a href="#187-order-by%E5%AD%90%E5%8F%A5">18.7 order by子句</a></li>
</ul>
</li>
<li><a href="#19%E8%BF%9E%E6%8E%A5%E6%9F%A5%E8%AF%A2">19、连接查询</a><ul>
<li><a href="#191-%E4%BA%A4%E5%8F%89%E8%BF%9E%E6%8E%A5">19.1 交叉连接</a></li>
<li><a href="#192-%E5%86%85%E8%BF%9E%E6%8E%A5">19.2 内连接</a></li>
<li><a href="#193-%E5%A4%96%E9%93%BE%E6%8E%A5">19.3 外链接</a></li>
<li><a href="#194-%E8%87%AA%E7%84%B6%E8%BF%9E%E6%8E%A5">19.4 自然连接</a></li>
</ul>
</li>
<li><a href="#20%E5%A4%96%E9%94%AE">20、外键</a><ul>
<li><a href="#201-%E5%A4%96%E9%94%AE%E6%93%8D%E4%BD%9C">20.1 外键操作</a></li>
<li><a href="#202-%E5%A4%96%E9%94%AE%E4%BD%9C%E7%94%A8">20.2 外键作用</a></li>
<li><a href="#203-%E5%A4%96%E9%94%AE%E6%9D%A1%E4%BB%B6">20.3 外键条件</a></li>
<li><a href="#204-%E5%A4%96%E9%94%AE%E7%BA%A6%E6%9D%9F">20.4 外键约束</a></li>
</ul>
</li>
<li><a href="#21%E8%81%94%E5%90%88%E6%9F%A5%E8%AF%A2">21、联合查询</a></li>
<li><a href="#22%E5%AD%90%E6%9F%A5%E8%AF%A2">22、子查询</a><ul>
<li><a href="#221-%E6%A0%87%E9%87%8F%E5%AD%90%E6%9F%A5%E8%AF%A2">22.1 标量子查询</a></li>
<li><a href="#222-%E5%88%97%E5%AD%90%E6%9F%A5%E8%AF%A2">22.2 列子查询</a></li>
<li><a href="#223-%E8%A1%8C%E5%AD%90%E6%9F%A5%E8%AF%A2">22.3 行子查询</a></li>
<li><a href="#224-%E8%A1%A8%E5%AD%90%E6%9F%A5%E8%AF%A2">22.4 表子查询</a></li>
<li><a href="#225-exists%E5%AD%90%E6%9F%A5%E8%AF%A2">22.5 exists子查询</a></li>
</ul>
</li>
<li><a href="#23%E8%A7%86%E5%9B%BE">23、视图</a><ul>
<li><a href="#231-%E5%88%9B%E5%BB%BA%E8%A7%86%E5%9B%BE">23.1 创建视图</a></li>
<li><a href="#232-%E6%9F%A5%E8%AF%A2%E8%A7%86%E5%9B%BE">23.2 查询视图</a></li>
<li><a href="#233-%E4%BD%BF%E7%94%A8%E8%A7%86%E5%9B%BE">23.3 使用视图</a></li>
<li><a href="#234-%E4%BF%AE%E6%94%B9%E8%A7%86%E5%9B%BE">23.4 修改视图</a></li>
<li><a href="#235-%E5%88%A0%E9%99%A4%E8%A7%86%E5%9B%BE">23.5 删除视图</a></li>
<li><a href="#236-%E8%A7%86%E5%9B%BE%E6%84%8F%E4%B9%89">23.6 视图意义</a></li>
</ul>
</li>
<li><a href="#24%E8%A7%86%E5%9B%BE%E6%95%B0%E6%8D%AE%E6%93%8D%E4%BD%9C">24、视图数据操作</a><ul>
<li><a href="#241-%E6%96%B0%E5%A2%9E%E6%95%B0%E6%8D%AE">24.1 新增数据</a></li>
<li><a href="#242-%E5%88%A0%E9%99%A4%E6%95%B0%E6%8D%AE">24.2 删除数据</a></li>
<li><a href="#243-%E6%9B%B4%E6%96%B0%E6%95%B0%E6%8D%AE">24.3 更新数据</a></li>
<li><a href="#244-%E8%A7%86%E5%9B%BE%E7%AE%97%E6%B3%95">24.4 视图算法</a></li>
</ul>
</li>
<li><a href="#25%E6%95%B0%E6%8D%AE%E5%A4%87%E4%BB%BD%E4%B8%8E%E8%BF%98%E5%8E%9F">25、数据备份与还原</a><ul>
<li><a href="#251-%E6%95%B0%E6%8D%AE%E8%A1%A8%E5%A4%87%E4%BB%BD">25.1 数据表备份</a></li>
<li><a href="#252-%E5%8D%95%E6%95%B0%E6%8D%AE%E8%A1%A8%E5%A4%87%E4%BB%BD">25.2 单数据表备份</a></li>
<li><a href="#253-sql%E5%A4%87%E4%BB%BD">25.3 SQL备份</a></li>
<li><a href="#254-%E5%A2%9E%E9%87%8F%E5%A4%87%E4%BB%BD">25.4 增量备份</a></li>
</ul>
</li>
<li><a href="#26%E4%BA%8B%E5%8A%A1">26、事务</a><ul>
<li><a href="#261-%E4%BA%8B%E5%8A%A1%E6%93%8D%E4%BD%9C">26.1 事务操作</a></li>
<li><a href="#262-%E4%BA%8B%E5%8A%A1%E5%8E%9F%E7%90%86">26.2 事务原理</a></li>
<li><a href="#263-%E5%9B%9E%E6%BB%9A%E7%82%B9">26.3 回滚点</a></li>
<li><a href="#264-%E8%87%AA%E5%8A%A8%E4%BA%8B%E5%8A%A1">26.4 自动事务</a></li>
<li><a href="#265-%E4%BA%8B%E5%8A%A1%E7%89%B9%E6%80%A7">26.5 事务特性</a></li>
</ul>
</li>
<li><a href="#27%E6%95%B0%E6%8D%AE%E5%BA%93%E5%8F%98%E9%87%8F">27、数据库变量</a><ul>
<li><a href="#271-%E7%B3%BB%E7%BB%9F%E5%8F%98%E9%87%8F">27.1 系统变量</a></li>
<li><a href="#272-%E8%87%AA%E5%AE%9A%E4%B9%89%E5%8F%98%E9%87%8F">27.2 自定义变量</a></li>
</ul>
</li>
<li><a href="#28%E8%A7%A6%E5%8F%91%E5%99%A8">28、触发器</a><ul>
<li><a href="#281-%E5%88%9B%E5%BB%BA%E8%A7%A6%E5%8F%91%E5%99%A8">28.1 创建触发器</a></li>
<li><a href="#282-%E6%9F%A5%E8%AF%A2%E8%A7%A6%E5%8F%91%E5%99%A8">28.2 查询触发器</a></li>
<li><a href="#283-%E4%BD%BF%E7%94%A8%E8%A7%A6%E5%8F%91%E5%99%A8">28.3 使用触发器</a></li>
<li><a href="#284-%E4%BF%AE%E6%94%B9%E8%A7%A6%E5%8F%91%E5%99%A8--%E5%88%A0%E9%99%A4%E8%A7%A6%E5%8F%91%E5%99%A8">28.4 修改触发器 &amp; 删除触发器</a></li>
<li><a href="#285-%E8%A7%A6%E5%8F%91%E5%99%A8%E8%AE%B0%E5%BD%95">28.5 触发器记录</a></li>
</ul>
</li>
<li><a href="#29%E4%BB%A3%E7%A0%81%E6%89%A7%E8%A1%8C%E7%BB%93%E6%9E%84">29、代码执行结构</a><ul>
<li><a href="#291-%E5%88%86%E6%94%AF%E7%BB%93%E6%9E%84">29.1 分支结构</a></li>
<li><a href="#292-%E5%BE%AA%E7%8E%AF%E7%BB%93%E6%9E%84">29.2 循环结构</a></li>
</ul>
</li>
<li><a href="#30%E5%87%BD%E6%95%B0">30、函数</a><ul>
<li><a href="#301-%E7%B3%BB%E7%BB%9F%E5%87%BD%E6%95%B0">30.1 系统函数</a></li>
<li><a href="#302-%E8%87%AA%E5%AE%9A%E4%B9%89%E5%87%BD%E6%95%B0">30.2 自定义函数</a></li>
<li><a href="#303-%E5%88%9B%E5%BB%BA%E5%87%BD%E6%95%B0">30.3 创建函数</a></li>
<li><a href="#304-%E6%9F%A5%E7%9C%8B%E5%87%BD%E6%95%B0">30.4 查看函数</a></li>
<li><a href="#305-%E4%BF%AE%E6%94%B9%E5%87%BD%E6%95%B0--%E5%88%A0%E9%99%A4%E5%87%BD%E6%95%B0">30.5 修改函数 &amp; 删除函数</a></li>
<li><a href="#306-%E5%87%BD%E6%95%B0%E5%8F%82%E6%95%B0">30.6 函数参数</a></li>
<li><a href="#307-%E5%8F%98%E9%87%8F%E4%BD%9C%E7%94%A8%E5%9F%9F">30.7 变量作用域</a></li>
</ul>
</li>
<li><a href="#31%E5%AD%98%E5%82%A8%E8%BF%87%E7%A8%8B">31、存储过程</a><ul>
<li><a href="#311-%E5%88%9B%E5%BB%BA%E8%BF%87%E7%A8%8B">31.1 创建过程</a></li>
<li><a href="#322-%E6%9F%A5%E7%9C%8B%E8%BF%87%E7%A8%8B">32.2 查看过程</a></li>
<li><a href="#323-%E8%B0%83%E7%94%A8%E8%BF%87%E7%A8%8B">32.3 调用过程</a></li>
<li><a href="#333-%E4%BF%AE%E6%94%B9%E8%BF%87%E7%A8%8B--%E5%88%A0%E9%99%A4%E8%BF%87%E7%A8%8B">33.3 修改过程 &amp; 删除过程</a></li>
<li><a href="#334-%E8%BF%87%E7%A8%8B%E5%8F%82%E6%95%B0">33.4 过程参数</a></li>
</ul>
</li>
</ul>
<hr>
<h1 id="1、SQL语言主要分为三类"><a href="#1、SQL语言主要分为三类" class="headerlink" title="1、SQL语言主要分为三类"></a>1、SQL语言主要分为三类</h1><ul>
<li>DDL：Data Defination Language，数据定义语言，用来维护存储数据的结构（数据库、表），例如create、drop和alter操作</li>
<li>DML：Data  Manipulation Language，数据操作语言，用来对数据进行操作，例如delete、update、insert，又可细分为DQL（Data Query Language），数据查询语言，例如select</li>
<li>DCL：Data Control Language，数据控制语言，用于负责用户权限管理，例如grant、revoke等</li>
</ul>
<hr>
<h1 id="2、库操作"><a href="#2、库操作" class="headerlink" title="2、库操作"></a>2、库操作</h1><h2 id="2-1-新增数据库"><a href="#2-1-新增数据库" class="headerlink" title="2.1 新增数据库"></a>2.1 新增数据库</h2><p>基本语法：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">create</span> <span class="keyword">database</span> 数据库名称 数据库选项;</span><br></pre></td></tr></table></figure></p>
<p>数据库选项：</p>
<ul>
<li>字符集设定：charset + 字符集，用来表示数据存储的编码格式，常用的字符集包括GBK和UTF8等</li>
<li>校对集设定：collate + 具体校对集，表示数据比较的规则，其依赖字符集<br>实例：<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">create</span> <span class="keyword">database</span> mydatabase <span class="keyword">charset</span> utf8;</span><br></pre></td></tr></table></figure>
</li>
</ul>
<p>注意：</p>
<ul>
<li>数据库名称最好别不要使用中文</li>
<li>数据库名称不要用关键字和保留字</li>
<li>如果要使用关键字和保留字，用反引号括起来</li>
</ul>
<hr>
<h2 id="2-2-查询数据库"><a href="#2-2-查询数据库" class="headerlink" title="2.2 查询数据库"></a>2.2 查询数据库</h2><p>基本语法：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">show</span> <span class="keyword">databases</span>;</span><br></pre></td></tr></table></figure></p>
<p>模糊查询语法：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">show</span> <span class="keyword">databases</span> <span class="keyword">like</span> <span class="string">'pattern'</span>;</span><br></pre></td></tr></table></figure></p>
<p>其中，pattern是匹配模式，有两种，分别是：</p>
<ul>
<li>%：表示匹配多个字符</li>
<li>_：表示匹配单个字符</li>
</ul>
<p>如果库名中有<em>时，要用\</em>进行转义。</p>
<hr>
<h2 id="2-3-更新数据库"><a href="#2-3-更新数据库" class="headerlink" title="2.3 更新数据库"></a>2.3 更新数据库</h2><p>基本语法：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">alter</span> dabase 数据库名称 数据选项</span><br></pre></td></tr></table></figure></p>
<p>实例：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">alter</span> <span class="keyword">database</span> mydatabase <span class="keyword">charset</span> gdk;</span><br></pre></td></tr></table></figure></p>
<p>注意：</p>
<ul>
<li>数据库名称不可更改</li>
</ul>
<hr>
<h2 id="2-4-删除数据库"><a href="#2-4-删除数据库" class="headerlink" title="2.4 删除数据库"></a>2.4 删除数据库</h2><p>基本语法：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">drop</span> <span class="keyword">database</span> 数据库名称;</span><br></pre></td></tr></table></figure></p>
<p>注意：</p>
<ul>
<li>删除是不可逆操作，删除之前要先备份数据库</li>
</ul>
<hr>
<h1 id="3、表操作"><a href="#3、表操作" class="headerlink" title="3、表操作"></a>3、表操作</h1><h2 id="3-1-新增表"><a href="#3-1-新增表" class="headerlink" title="3.1 新增表"></a>3.1 新增表</h2><p>基本语法：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">create</span> <span class="keyword">table</span> [<span class="keyword">if</span> <span class="keyword">not</span> <span class="keyword">exists</span>] 表名(</span><br><span class="line">    字段名称 数据类型;</span><br><span class="line">    ...</span><br><span class="line">    字段名称 数据类型</span><br><span class="line">)[表选项];</span><br></pre></td></tr></table></figure></p>
<p>其中，if not exists 表示：</p>
<ul>
<li>如果表名不存在，就执行创建代码；如果表名存在，则不执行创建代码。</li>
</ul>
<p>表选项则是用来控制表的表现形式的，共有三种，分别为：</p>
<ul>
<li>字符集设定：charset 具体字符集，用来指定存储的数据的编码格式，常用的有GBK和UTF8</li>
<li>校对集设定：collate 具体校对集，表示数据比较的规则，其依赖字符集</li>
<li>存储引擎：engine 具体存储引擎，默认为InnoDB，常用的还有MyISAM</li>
</ul>
<p>实例一：实现的指定表所属的数据库<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">create</span> <span class="keyword">table</span> <span class="keyword">if</span> <span class="keyword">not</span> <span class="keyword">exists</span> test.Student(</span><br><span class="line">    <span class="keyword">name</span> <span class="built_in">varchar</span>(<span class="number">10</span>),</span><br><span class="line">    age <span class="built_in">int</span>,</span><br><span class="line">    grade <span class="built_in">varchar</span>(<span class="number">10</span>)</span><br><span class="line">) <span class="keyword">engine</span> <span class="keyword">InnoDB</span> <span class="keyword">charset</span> utf8;</span><br></pre></td></tr></table></figure></p>
<p>实例二：隐式的指定表所属的数据库<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">use</span> <span class="keyword">test</span>;</span><br><span class="line"><span class="keyword">create</span> <span class="keyword">table</span> <span class="keyword">if</span> <span class="keyword">not</span> <span class="keyword">exists</span> Student(</span><br><span class="line">    <span class="keyword">name</span> <span class="built_in">varchar</span>(<span class="number">10</span>),</span><br><span class="line">    age <span class="built_in">int</span>,</span><br><span class="line">    grade <span class="built_in">varchar</span>(<span class="number">10</span>)</span><br><span class="line">) <span class="keyword">engine</span> <span class="keyword">InnoDB</span> <span class="keyword">charset</span> utf8;</span><br></pre></td></tr></table></figure></p>
<hr>
<h2 id="3-2-查询表"><a href="#3-2-查询表" class="headerlink" title="3.2 查询表"></a>3.2 查询表</h2><p>基本语法：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">show</span> <span class="keyword">tables</span>;</span><br></pre></td></tr></table></figure></p>
<p>模糊查询语法：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">show</span> <span class="keyword">tables</span> <span class="keyword">like</span> <span class="string">'pattern'</span>;</span><br></pre></td></tr></table></figure></p>
<p>其中，pattern是匹配模式，有两种，分别是：</p>
<ul>
<li>%：表示匹配多个字符</li>
<li>_：表示匹配单个字符</li>
</ul>
<p>查看表中的字段信息：<br><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">desc/describe/show columns from 表名;</span><br></pre></td></tr></table></figure></p>
<hr>
<h2 id="3-3-更新表"><a href="#3-3-更新表" class="headerlink" title="3.3 更新表"></a>3.3 更新表</h2><p>表的修改，分为修改表本身和修改表中的字段。<br>第1类：修改表本身</p>
<ul>
<li><p>修改表名，基本语法：</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">rename</span> <span class="keyword">table</span> 旧表名 <span class="keyword">to</span> 新表名;</span><br></pre></td></tr></table></figure>
</li>
<li><p>修改表选项，基本语法：</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">alter</span> <span class="keyword">table</span> 表名 表选项[=] 值;</span><br></pre></td></tr></table></figure>
</li>
</ul>
<p>第2类：修改表中的字段，新增、修改、重命名和删除</p>
<ul>
<li>新增字段，基本语法：<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">alter</span> <span class="keyword">table</span> 表名 <span class="keyword">add</span> [<span class="keyword">column</span>] 字段名 数据类型 [列属性][位置];</span><br></pre></td></tr></table></figure>
</li>
</ul>
<p>其中，位置表示此字段存储的位置，分为first（第一个位置）和after + 字段名（指定的字段后，默认为最后一个位置）。<br>实例：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">alter</span> <span class="keyword">table</span> Student <span class="keyword">add</span> <span class="keyword">column</span> <span class="keyword">id</span> <span class="built_in">int</span> <span class="keyword">first</span>;</span><br></pre></td></tr></table></figure></p>
<ul>
<li>修改字段，基本语法：<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">alter</span> <span class="keyword">table</span> 表名 <span class="keyword">modify</span> 字段名 数据类型 [列属性][位置];</span><br></pre></td></tr></table></figure>
</li>
</ul>
<p>实例：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">alter</span> <span class="keyword">table</span> Student <span class="keyword">modify</span> <span class="keyword">name</span> <span class="built_in">char</span>(<span class="number">10</span>) <span class="keyword">after</span> <span class="keyword">id</span>;</span><br></pre></td></tr></table></figure></p>
<ul>
<li>重命名字段，基本语法：<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">alter</span> <span class="keyword">table</span> 表名 <span class="keyword">change</span> 旧字段名 新字段名 数据类型 [列属性][位置];</span><br></pre></td></tr></table></figure>
</li>
</ul>
<p>实例：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">alter</span> <span class="keyword">table</span> Student <span class="keyword">change</span> grage <span class="keyword">class</span> <span class="built_in">varchar</span>(<span class="number">10</span>);</span><br></pre></td></tr></table></figure></p>
<hr>
<ul>
<li>删除字段，基本语法：<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">alter</span> <span class="keyword">table</span> 表名 <span class="keyword">drop</span> 字段名;</span><br></pre></td></tr></table></figure>
</li>
</ul>
<p>实例：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">alter</span> <span class="keyword">table</span> Student <span class="keyword">drop</span> age;</span><br></pre></td></tr></table></figure></p>
<p>注意：</p>
<ul>
<li>如果表中已经存在数据，那么删除该字段会清空该字段所有的数据，且不可逆，慎用。</li>
</ul>
<hr>
<h2 id="3-4-删除表"><a href="#3-4-删除表" class="headerlink" title="3.4 删除表"></a>3.4 删除表</h2><p>基本语法：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">drop</span> <span class="keyword">table</span> 表<span class="number">1</span>, 表<span class="number">2</span>;</span><br></pre></td></tr></table></figure></p>
<p> 注意：</p>
<ul>
<li>删除表为不可逆操作，慎用。</li>
</ul>
<hr>
<h1 id="4、数据操作"><a href="#4、数据操作" class="headerlink" title="4、数据操作"></a>4、数据操作</h1><p>SQL的基本操作CURD，即增删改查</p>
<h2 id="4-1-新增数据"><a href="#4-1-新增数据" class="headerlink" title="4.1 新增数据"></a>4.1 新增数据</h2><p>对于数据的新增操作，有两种方法。<br>第1种：给全表字段插入数据，不需要指定字段列表，但要求数据的值出现的顺序与表中的字段出现的顺序一致，而且凡是非数值数据，都需要用引号括起来。<br>基本语法：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> 表名 <span class="keyword">values</span>(值列表[,(值列表)]);</span><br></pre></td></tr></table></figure></p>
<p>实例：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> Student <span class="keyword">values</span>(<span class="number">1</span>, <span class="string">'Harlon'</span>, <span class="string">'class-1'</span>);</span><br></pre></td></tr></table></figure></p>
<p>第2种：给部分字段插入数据，需要选定字段列表，字段列表中字段出现的顺序与表中字段的顺序无关，但值列表的顺序必须与字段列表的保持一致。<br>基本语法：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> 表名(字段列表) <span class="keyword">values</span>(值列表[,(值列表)]);</span><br></pre></td></tr></table></figure></p>
<p>实例：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> Student(<span class="keyword">name</span>, <span class="keyword">class</span>) <span class="keyword">values</span>(<span class="string">'Juice'</span>, <span class="string">'class-2'</span>);</span><br></pre></td></tr></table></figure></p>
<hr>
<h2 id="4-2-查询数据"><a href="#4-2-查询数据" class="headerlink" title="4.2 查询数据"></a>4.2 查询数据</h2><p>基本语法：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> * <span class="keyword">from</span> 表名 [<span class="keyword">where</span> 条件];</span><br></pre></td></tr></table></figure></p>
<p>查看部分，基本语法：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> 字段名称[,字段名称] <span class="keyword">from</span> 表名 [<span class="keyword">where</span> 条件];</span><br></pre></td></tr></table></figure></p>
<p>实例：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> <span class="keyword">name</span>, <span class="keyword">class</span> <span class="keyword">from</span> Student;</span><br></pre></td></tr></table></figure></p>
<hr>
<h2 id="4-3-更新数据"><a href="#4-3-更新数据" class="headerlink" title="4.3 更新数据"></a>4.3 更新数据</h2><p>基础语法：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">update</span> 表名 字段 = 值 [<span class="keyword">where</span> 条件];</span><br></pre></td></tr></table></figure></p>
<p>在这里，尽量加上where条件，否则的话，操作就是全表数据。<br>实例：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">update</span> Student <span class="keyword">set</span> <span class="keyword">class</span> = <span class="string">'class-2'</span> <span class="keyword">where</span> <span class="keyword">id</span>=<span class="number">1</span>;</span><br></pre></td></tr></table></figure></p>
<hr>
<h2 id="4-3-删除数据"><a href="#4-3-删除数据" class="headerlink" title="4.3 删除数据"></a>4.3 删除数据</h2><p>基本语法：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">delete</span> <span class="keyword">from</span> 表名 [<span class="keyword">where</span> 条件];</span><br></pre></td></tr></table></figure></p>
<p>实例：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">delete</span> <span class="keyword">from</span> Strudent <span class="keyword">where</span> <span class="keyword">name</span>=<span class="string">'Juice'</span>;</span><br></pre></td></tr></table></figure></p>
<p>我们也可以用drop实现删除操作，不过与delete相比，drop威力更强，其在执行删除操作的时候，不仅会删除数据，还会删除定义并释放存储空间；而delete删除的时候，仅会删除数据，并不会删除定义和释放存储空间。</p>
<hr>
<h1 id="5、字符集问题"><a href="#5、字符集问题" class="headerlink" title="5、字符集问题"></a>5、字符集问题</h1><p>查看服务器识别的字符集<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">show</span> <span class="built_in">character</span> <span class="keyword">set</span>;</span><br></pre></td></tr></table></figure></p>
<p>查看服务器默认的对外处理的字符集<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">show</span> <span class="keyword">variables</span> <span class="keyword">like</span> <span class="string">'character_set%'</span>;</span><br></pre></td></tr></table></figure></p>
<p>修改字符传输方式：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">set</span> <span class="keyword">names</span> utf8;</span><br></pre></td></tr></table></figure></p>
<hr>
<h1 id="6、校对集问题"><a href="#6、校对集问题" class="headerlink" title="6、校对集问题"></a>6、校对集问题</h1><p>校对集有三种：</p>
<ul>
<li>_bin：binary，二进制比较，区分大小写</li>
<li>_cs：case sensitive，大小写敏感，区分大小写</li>
<li>_ci：case insensitive，大小写不敏感，不区分大小写</li>
</ul>
<p>查看全部校对集：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">show</span> <span class="keyword">collation</span>;</span><br></pre></td></tr></table></figure></p>
<p>注意：</p>
<ul>
<li>如果在没有数据之前没有声明校对集，在有了数据之后，再对校对集进行修改，则修改无效</li>
</ul>
<hr>
<h1 id="7、数值型"><a href="#7、数值型" class="headerlink" title="7、数值型"></a>7、数值型</h1><p>在SQL中，将数据类型分为三大类，分别为：数值型、字符串型和日期时间型<br><img src="https://img-blog.csdn.net/20170505201016682" alt></p>
<h2 id="7-1-整数型"><a href="#7-1-整数型" class="headerlink" title="7.1 整数型"></a>7.1 整数型</h2><p>在SQL中，由于要考虑节省磁盘空间的问题，因此系统又将整型分配五类，分别为：</p>
<ul>
<li>tinyint：迷你整型，使用1个字节存储数据（常用）</li>
<li>smallint：小整型，使用2个字节存储数据</li>
<li>mediumint：中整型，使用3个字节存储数据</li>
<li>int：标准整型，使用4个字节存储数据（常用）</li>
<li>bigint：大整型，使用8个字节存储数据</li>
</ul>
<p>实例：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">create</span> <span class="keyword">table</span> my_int( </span><br><span class="line">    int_1 <span class="built_in">tinyint</span>, </span><br><span class="line">    int_2 <span class="built_in">smallint</span>, </span><br><span class="line">    int_3 <span class="built_in">int</span>, </span><br><span class="line">    int_4 <span class="built_in">bigint</span> </span><br><span class="line">) <span class="keyword">engine</span> <span class="keyword">InnoDB</span> <span class="keyword">charset</span> utf8;</span><br></pre></td></tr></table></figure></p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">mysql&gt; desc my_int;</span><br><span class="line">+<span class="comment">-------+-------------+------+-----+---------+-------+</span></span><br><span class="line">| Field | Type        | Null | Key | Default | Extra |</span><br><span class="line">+<span class="comment">-------+-------------+------+-----+---------+-------+</span></span><br><span class="line">| int_1 | tinyint(4)  | YES  |     | NULL    |       |</span><br><span class="line">| int_2 | smallint(6) | YES  |     | NULL    |       |</span><br><span class="line">| int_3 | int(11)     | YES  |     | NULL    |       |</span><br><span class="line">| int_4 | bigint(20)  | YES  |     | NULL    |       |</span><br><span class="line">+<span class="comment">-------+-------------+------+-----+---------+-------+</span></span><br></pre></td></tr></table></figure>
<p>每个字段的数据后面都会跟一堆括号，并且里面含有数字，这表示数据的显示宽度。<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">mysql&gt; alter table my_int add int_5 tinyint zerofill;</span><br><span class="line">Query OK, 0 rows affected (0.05 sec)</span><br><span class="line">Records: 0  Duplicates: 0  Warnings: 0</span><br><span class="line">mysql&gt; desc my_int;</span><br><span class="line">+<span class="comment">-------+------------------------------+------+-----+---------+-------+</span></span><br><span class="line">| Field | Type                         | Null | Key | Default | Extra |</span><br><span class="line">+<span class="comment">-------+------------------------------+------+-----+---------+-------+</span></span><br><span class="line">| int_1 | tinyint(4)                   | YES  |     | NULL    |       |</span><br><span class="line">| int_2 | smallint(6)                  | YES  |     | NULL    |       |</span><br><span class="line">| int_3 | int(11)                      | YES  |     | NULL    |       |</span><br><span class="line">| int_4 | bigint(20)                   | YES  |     | NULL    |       |</span><br><span class="line">| int_5 | tinyint(3) unsigned zerofill | YES  |     | NULL    |       |</span><br><span class="line">+<span class="comment">-------+------------------------------+------+-----+---------+-------+</span></span><br><span class="line">5 rows in <span class="keyword">set</span> (<span class="number">0.00</span> sec)</span><br></pre></td></tr></table></figure></p>
<hr>
<h2 id="7-2-小数型"><a href="#7-2-小数型" class="headerlink" title="7.2 小数型"></a>7.2 小数型</h2><p>在SQL中，将小数型分为浮点型和顶点型两种，其中：</p>
<ul>
<li>浮点型：小数点浮动，精度有限，容易丢失精度</li>
<li>定点型：小数点固定，精度固定，不会丢失精度</li>
</ul>
<p>第1种：浮点型<br>浮点型数据是一种精度型数据，因为超出指定范围之后，其会丢失精度，自动进行四舍五入操作，分为两种精度：</p>
<ul>
<li>float：单精度，占用4个字节存储数据，精度范围大概7位左右</li>
<li>double：双精度，占用8个字节存储数据，精度范围15位左右</li>
</ul>
<p>浮点型使用方式：如果直接使用float，则表示没有小数部分；如果用float(M, D)，其中M代表总长度，D代表小数部分长度，M-D则为证书部分长度。<br>实例：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">create</span> <span class="keyword">table</span> my_float(</span><br><span class="line">    f1 <span class="built_in">float</span>,</span><br><span class="line">    f2 <span class="built_in">float</span>(<span class="number">10</span>, <span class="number">2</span>),</span><br><span class="line">    f3 <span class="built_in">float</span>(<span class="number">6</span>, <span class="number">2</span>)</span><br><span class="line">) <span class="keyword">engine</span> <span class="keyword">InnoDB</span> <span class="keyword">charset</span> utf8;</span><br></pre></td></tr></table></figure></p>
<p>插入浮点数时，可以直接插入小数，也可以插入科学计数法表示的数据，此外，插入浮点型数据时，整数部分不能超出长度范围，但小数部分可以超出长度范围的，系统会自动进行四舍五入操作。<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">mysql&gt; insert into my_float values(2.15e4, 20.15, 9999.99);</span><br><span class="line">Query OK, 1 row affected (0.00 sec)</span><br><span class="line"></span><br><span class="line">mysql&gt; insert into my_float values(20180710, 33.338888, 9999.99);</span><br><span class="line">Query OK, 1 row affected (0.00 sec)</span><br><span class="line"></span><br><span class="line">mysql&gt; select * from my_float;</span><br><span class="line">+<span class="comment">----------+-------+---------+</span></span><br><span class="line">| f1       | f2    | f3      |</span><br><span class="line">+<span class="comment">----------+-------+---------+</span></span><br><span class="line">|    21500 | 20.15 | 9999.99 |</span><br><span class="line">| 20180700 | 33.34 | 9999.99 |</span><br><span class="line">+<span class="comment">----------+-------+---------+</span></span><br><span class="line">2 rows in <span class="keyword">set</span> (<span class="number">0.00</span> sec)</span><br></pre></td></tr></table></figure></p>
<p>第2种：定点型<br>定点型数据，绝对的保证整数部分不会被四舍五入，也就是说不会丢失精度，但小数部分有可能会丢失精度，虽然理论上小数部分也不会丢失精度。<br>实例：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">create</span> <span class="keyword">table</span> my_decimal (</span><br><span class="line">    f1 <span class="built_in">float</span>(<span class="number">10</span>, <span class="number">2</span>),</span><br><span class="line">    d1 <span class="built_in">decimal</span>(<span class="number">10</span>, <span class="number">2</span>)</span><br><span class="line">) <span class="keyword">engine</span> <span class="keyword">InnoDB</span> <span class="keyword">charset</span> utf8;</span><br></pre></td></tr></table></figure></p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">mysql&gt; insert into my_decimal values(99999999.99, 99999999.99);</span><br><span class="line">Query OK, 1 row affected (0.01 sec)</span><br><span class="line"></span><br><span class="line">mysql&gt; insert into my_decimal values(123456.99, 2018.1364);</span><br><span class="line">Query OK, 1 row affected, 1 warning (0.00 sec)</span><br><span class="line"></span><br><span class="line">mysql&gt; select * from my_decimal;</span><br><span class="line">+<span class="comment">--------------+-------------+</span></span><br><span class="line">| f1           | d1          |</span><br><span class="line">+<span class="comment">--------------+-------------+</span></span><br><span class="line">| 100000000.00 | 99999999.99 |</span><br><span class="line">|    123456.99 |     2018.14 |</span><br><span class="line">+<span class="comment">--------------+-------------+</span></span><br><span class="line">2 rows in <span class="keyword">set</span> (<span class="number">0.00</span> sec)</span><br></pre></td></tr></table></figure>
<hr>
<h1 id="8、日期时间类型"><a href="#8、日期时间类型" class="headerlink" title="8、日期时间类型"></a>8、日期时间类型</h1><p>日期时间类型共有物种类型，分别为：</p>
<ul>
<li>datetime：日期时间，其格式为yyyy-MM-dd HH:mm:ss，表示的范围是1000年到9999年，有零值，即0000-00-00 00:00:00</li>
<li>date：日期，就是datetime的date部分</li>
<li>time：时间，或者说是时间段，为指定的某个时间区间之间，包含正负时间；</li>
<li>timestamp：时间戳，但并不是真正意义上的时间戳，其是从1970年开始计算的，格式和datetime一致</li>
<li>year：年份，有两种格式，分别为year(2)和year(4)</li>
</ul>
<p>实例：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">create</span> <span class="keyword">table</span> my_date(</span><br><span class="line">    d1 datetime,</span><br><span class="line">    d2 <span class="built_in">date</span>,</span><br><span class="line">    d3 <span class="built_in">time</span>,</span><br><span class="line">    d4 <span class="built_in">timestamp</span>,</span><br><span class="line">    d5 <span class="keyword">year</span></span><br><span class="line">) <span class="keyword">engine</span> <span class="keyword">InnoDB</span> <span class="keyword">charset</span> utf8;</span><br></pre></td></tr></table></figure></p>
<p>日期时间型中的time，可以为负数，甚至可以是很大的负数；year，可以使用 2 位数据插入，也可以使用 4 位数据插入；timestamp，只要当前所在的记录被更新，该字段就会自动更新为当前时间，且时间戳类型默认为非空的。<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">mysql&gt; insert into my_date values('2018-07-10 13:15:00', '2017-07-10', '18:49:00', '2018-07-10 13:15:00', 2018);</span><br><span class="line">Query OK, 1 row affected (0.00 sec)</span><br><span class="line"></span><br><span class="line">mysql&gt; insert into my_date values('2018-07-10 13:15:00', '2017-07-10', '-135:49:00', '2018-07-10 13:15:00', 69);</span><br><span class="line">Query OK, 1 row affected (0.01 sec)</span><br><span class="line"></span><br><span class="line">mysql&gt; insert into my_date values('2018-07-10 13:15:00', '2017-07-10', '-2 35:49:00', '2018-07-10 13:15:00', 70);</span><br><span class="line">Query OK, 1 row affected (0.04 sec)</span><br><span class="line"></span><br><span class="line">mysql&gt; select * from my_date;</span><br><span class="line">+<span class="comment">---------------------+------------+------------+---------------------+------+</span></span><br><span class="line">| d1                  | d2         | d3         | d4                  | d5   |</span><br><span class="line">+<span class="comment">---------------------+------------+------------+---------------------+------+</span></span><br><span class="line">| 2018-07-10 13:15:00 | 2017-07-10 | 18:49:00   | 2018-07-10 13:15:00 | 2018 |</span><br><span class="line">| 2018-07-10 13:15:00 | 2017-07-10 | -135:49:00 | 2018-07-10 13:15:00 | 2069 |</span><br><span class="line">| 2018-07-10 13:15:00 | 2017-07-10 | -83:49:00  | 2018-07-10 13:15:00 | 1970 |</span><br><span class="line">+<span class="comment">---------------------+------------+------------+---------------------+------+</span></span><br><span class="line">3 rows in <span class="keyword">set</span> (<span class="number">0.00</span> sec)</span><br></pre></td></tr></table></figure></p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">mysql&gt; update my_date set d1 = '2017-07-10 18:54:00' where d5 = 1970;</span><br><span class="line">Query OK, 1 row affected (0.00 sec)</span><br><span class="line">Rows matched: 1  Changed: 1  Warnings: 0</span><br><span class="line"></span><br><span class="line">mysql&gt; select * from my_date;</span><br><span class="line">+<span class="comment">---------------------+------------+------------+---------------------+------+</span></span><br><span class="line">| d1                  | d2         | d3         | d4                  | d5   |</span><br><span class="line">+<span class="comment">---------------------+------------+------------+---------------------+------+</span></span><br><span class="line">| 2018-07-10 13:15:00 | 2017-07-10 | 18:49:00   | 2018-07-10 13:15:00 | 2018 |</span><br><span class="line">| 2018-07-10 13:15:00 | 2017-07-10 | -135:49:00 | 2018-07-10 13:15:00 | 2069 |</span><br><span class="line">| 2017-07-10 18:54:00 | 2017-07-10 | -83:49:00  | 2018-07-10 18:54:50 | 1970 |</span><br><span class="line">+<span class="comment">---------------------+------------+------------+---------------------+------+</span></span><br><span class="line">3 rows in <span class="keyword">set</span> (<span class="number">0.00</span> sec)</span><br></pre></td></tr></table></figure>
<hr>
<h1 id="9、字符类型"><a href="#9、字符类型" class="headerlink" title="9、字符类型"></a>9、字符类型</h1><p>在SQL中，将字符串分为6类，分别为：char、varchar、text、blob、enum和set<br>第1类：定长字符串<br>定长字符串：char，即磁盘在定义结构的时候就确定了最终数据的长度。</p>
<ul>
<li>char(L)：L表示length，即存储的长度，最大长度为255</li>
<li>char(4)：表示在utf8情况下，需要4*3 = 12字节</li>
</ul>
<p>第2类：变长字符串<br>变长字符串：varchar，即在分配空间的时候，按照最大的空间进行分配，但是实际用了多少，则是根据具体的数据来确定。</p>
<ul>
<li>varchar(L)：L表示length，理论长度是65536，但是会多出来1到2个字节来确定存储的实际长度</li>
<li>varchar(10)：例如存储10个汉字，在UTF8环境下，需要10*3+1 = 31个字节<br>实际上，如果超过255个字符，用text。<br>如何选择定长字符串或者变长字符串呢？</li>
<li>定长字符串对磁盘空间比较浪费，但是效率高；如果数据基本上确定长度都一样，就使用定长字符串，例如身份证、电话号码等</li>
<li>变长字符串对磁盘空间比较节省，但是效率低；如果数据不能确定长度（不同的数据有变化），就使用变长字符串，例如地址、姓名等</li>
</ul>
<p>第3类：文本字符串<br>如果数据量非常大，通常说超过255个字符就会使用文本字符串。<br>文本字符串根据存储的格式分类，可以分为：</p>
<ul>
<li>text：存储文字</li>
<li>blob：存储二进制数据（其实际上都是存储路径），通常不用</li>
</ul>
<p>第4类：枚举字符串<br>枚举字符串：enum，需要事先将可能出现的结果都设计好，实际上存储的数据必须是规定好的数据中的一个。<br>枚举字符串的使用方式：</p>
<ul>
<li>定义：enum(‘元素1’, ‘元素2’, ‘元素3’, …)</li>
<li>使用：存储的数据，只能是事先定义好的数据</li>
</ul>
<p>实例：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">create</span> <span class="keyword">table</span> my_enum(</span><br><span class="line">    gender enum(<span class="string">'男'</span>, <span class="string">'女'</span>, <span class="string">'保密'</span>)</span><br><span class="line">) <span class="keyword">engine</span> <span class="keyword">InnoDB</span> <span class="keyword">charset</span> utf8;</span><br></pre></td></tr></table></figure></p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">mysql&gt; insert into my_enum values('男'), ('女'), ('保密');</span><br><span class="line">Query OK, 3 rows affected (0.00 sec)</span><br><span class="line">Records: 3  Duplicates: 0  Warnings: 0</span><br><span class="line"></span><br><span class="line">mysql&gt; insert into my_enum values('male');</span><br><span class="line">ERROR 1265 (01000): Data truncated for column 'gender' at row 1</span><br><span class="line">mysql&gt; select * from my_enum;</span><br><span class="line">+<span class="comment">--------+</span></span><br><span class="line">| gender |</span><br><span class="line">+<span class="comment">--------+</span></span><br><span class="line">| 男     |</span><br><span class="line">| 女     |</span><br><span class="line">| 保密   |</span><br><span class="line">+<span class="comment">--------+</span></span><br><span class="line">3 rows in <span class="keyword">set</span> (<span class="number">0.00</span> sec)</span><br></pre></td></tr></table></figure>
<p>枚举除了规范数据格式外，还能节省空间，因为枚举存储的是一个数值，而不是字符串本身。枚举的效率不高。</p>
<p>第5类：集合字符串<br>集合字符串：set，跟枚举类似，实际存储的是数值而不是字符串。<br>集合字符串的使用方式：</p>
<ul>
<li>定义：set，元素列表；</li>
<li>使用：可以使用元素列表中的多个元素，用逗号分隔</li>
</ul>
<p>实例：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">create</span> <span class="keyword">table</span> my_set(</span><br><span class="line">    hobby <span class="keyword">set</span>(<span class="string">'音乐'</span>, <span class="string">'电影'</span>, <span class="string">'旅行'</span>, <span class="string">'美食'</span>, <span class="string">'摄影'</span>, <span class="string">'运动'</span>, <span class="string">'宠物'</span>)</span><br><span class="line">) <span class="keyword">engine</span> <span class="keyword">InnoDB</span> <span class="keyword">charset</span> utf8;</span><br></pre></td></tr></table></figure></p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">mysql&gt; insert into my_set values('电影,旅行,摄影');</span><br><span class="line">Query OK, 1 row affected (0.00 sec)</span><br><span class="line"></span><br><span class="line">mysql&gt; insert into my_set values(3);</span><br><span class="line">Query OK, 1 row affected (0.01 sec)</span><br><span class="line"></span><br><span class="line">mysql&gt; select * from my_set;</span><br><span class="line">+<span class="comment">----------------------+</span></span><br><span class="line">| hobby                |</span><br><span class="line">+<span class="comment">----------------------+</span></span><br><span class="line">| 电影,旅行,摄影       |</span><br><span class="line">| 音乐,电影            |</span><br><span class="line">+<span class="comment">----------------------+</span></span><br><span class="line">2 rows in <span class="keyword">set</span> (<span class="number">0.00</span> sec)</span><br></pre></td></tr></table></figure>
<p>同样，使用集合的效率并不高，但能规范数据和节省空间。</p>
<hr>
<h1 id="10、记录长度"><a href="#10、记录长度" class="headerlink" title="10、记录长度"></a>10、记录长度</h1><p>MySQL规定：任何一条记录最长不超过65535个字节，这意味着varchar永远达不到理论最大值。</p>
<hr>
<h1 id="11、列属性"><a href="#11、列属性" class="headerlink" title="11、列属性"></a>11、列属性</h1><p>列属性：实际上，真正约束字段的数据类型，但是数据类型的约束比较单一，因此就需要额外的一些约束来保证数据的有效性，这就是列属性。<br>列属性有很多：例如：null、not null、default、primary key、unique key、auto_increment和comment等。</p>
<h2 id="11-1-空属性"><a href="#11-1-空属性" class="headerlink" title="11.1 空属性"></a>11.1 空属性</h2><p>空属性有两个值，分别为：null和not null<br>虽然默认数据库的字段基本都为空，但是实际上真正开发的时候，要尽可能的保证数据不为空，因为空数据没有意义，也没办法参与运算。</p>
<p>实例：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">create</span> <span class="keyword">table</span> my_class (</span><br><span class="line">    grade <span class="built_in">varchar</span>(<span class="number">20</span>) <span class="keyword">not</span> <span class="literal">null</span>,</span><br><span class="line">    room <span class="built_in">varchar</span>(<span class="number">20</span>) <span class="literal">null</span> <span class="comment">-- 显示声明为空，实际上，默认就为空</span></span><br><span class="line">) <span class="keyword">engine</span> <span class="keyword">InnoDB</span> <span class="keyword">charset</span> utf8;</span><br></pre></td></tr></table></figure></p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">mysql&gt; desc my_class;</span><br><span class="line">+<span class="comment">-------+-------------+------+-----+---------+-------+</span></span><br><span class="line">| Field | Type        | Null | Key | Default | Extra |</span><br><span class="line">+<span class="comment">-------+-------------+------+-----+---------+-------+</span></span><br><span class="line">| grade | varchar(20) | NO   |     | NULL    |       |</span><br><span class="line">| room  | varchar(20) | YES  |     | NULL    |       |</span><br><span class="line">+<span class="comment">-------+-------------+------+-----+---------+-------+</span></span><br><span class="line">2 rows in <span class="keyword">set</span> (<span class="number">0.00</span> sec)</span><br></pre></td></tr></table></figure>
<hr>
<h2 id="11-2-列描述"><a href="#11-2-列描述" class="headerlink" title="11.2 列描述"></a>11.2 列描述</h2><p>列描述：comment，表示描述，没有实际含义，是专门用来存储描述字段的，其会随着创建语句自动保存，用来给程序员（数据库管理员）了解数据库使用。  </p>
<p>实例：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">create</span> <span class="keyword">table</span> my_friend(</span><br><span class="line">    <span class="keyword">name</span> <span class="built_in">varchar</span>(<span class="number">20</span>) <span class="keyword">not</span> <span class="literal">null</span> <span class="keyword">comment</span> <span class="string">'姓名'</span>,</span><br><span class="line">    age <span class="built_in">tinyint</span> <span class="keyword">not</span> <span class="literal">null</span> <span class="keyword">comment</span> <span class="string">'年龄'</span></span><br><span class="line">) <span class="keyword">engine</span> <span class="keyword">InnoDB</span> <span class="keyword">charset</span> utf8;</span><br></pre></td></tr></table></figure></p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">mysql&gt; desc my_friend;</span><br><span class="line">+<span class="comment">-------+-------------+------+-----+---------+-------+</span></span><br><span class="line">| Field | Type        | Null | Key | Default | Extra |</span><br><span class="line">+<span class="comment">-------+-------------+------+-----+---------+-------+</span></span><br><span class="line">| name  | varchar(20) | NO   |     | NULL    |       |</span><br><span class="line">| age   | tinyint(4)  | NO   |     | NULL    |       |</span><br><span class="line">+<span class="comment">-------+-------------+------+-----+---------+-------+</span></span><br><span class="line">2 rows in <span class="keyword">set</span> (<span class="number">0.00</span> sec)</span><br><span class="line"></span><br><span class="line">mysql&gt; <span class="keyword">show</span> <span class="keyword">create</span> <span class="keyword">table</span> my_friend;</span><br><span class="line">+<span class="comment">-----------+--------------------------------------------------------------------------------------------------------------------------------------------------------------+</span></span><br><span class="line">| Table     | <span class="keyword">Create</span> <span class="keyword">Table</span>                                                                                                                                                 |</span><br><span class="line">+<span class="comment">-----------+--------------------------------------------------------------------------------------------------------------------------------------------------------------+</span></span><br><span class="line">| my_friend | <span class="keyword">CREATE</span> <span class="keyword">TABLE</span> <span class="string">`my_friend`</span> (</span><br><span class="line">  <span class="string">`name`</span> <span class="built_in">varchar</span>(<span class="number">20</span>) <span class="keyword">NOT</span> <span class="literal">NULL</span> <span class="keyword">COMMENT</span> <span class="string">'姓名'</span>,</span><br><span class="line">  <span class="string">`age`</span> <span class="built_in">tinyint</span>(<span class="number">4</span>) <span class="keyword">NOT</span> <span class="literal">NULL</span> <span class="keyword">COMMENT</span> <span class="string">'年龄'</span></span><br><span class="line">) <span class="keyword">ENGINE</span>=<span class="keyword">InnoDB</span> <span class="keyword">DEFAULT</span> <span class="keyword">CHARSET</span>=utf8     |</span><br><span class="line">+<span class="comment">-----------+--------------------------------------------------------------------------------------------------------------------------------------------------------------+</span></span><br><span class="line"><span class="number">1</span> <span class="keyword">row</span> <span class="keyword">in</span> <span class="keyword">set</span> (<span class="number">0.00</span> sec)</span><br></pre></td></tr></table></figure>
<hr>
<h2 id="11-3-默认值"><a href="#11-3-默认值" class="headerlink" title="11.3 默认值"></a>11.3 默认值</h2><p>默认值：default，某一数据会经常性出现某个具体的值，因此可以在开始的时候就制定好，而在需要真实数据的时候，用户可以选择性的使用默认值。  </p>
<p>实例：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">create</span> <span class="keyword">table</span> my_default(</span><br><span class="line">    <span class="keyword">name</span> <span class="built_in">varchar</span>(<span class="number">20</span>) <span class="keyword">not</span> <span class="literal">null</span>,</span><br><span class="line">    age <span class="built_in">tinyint</span> <span class="keyword">unsigned</span> <span class="keyword">default</span> <span class="number">0</span>,</span><br><span class="line">    gender enum(<span class="string">'男'</span>, <span class="string">'女'</span>) <span class="keyword">default</span> <span class="string">'男'</span></span><br><span class="line">) <span class="keyword">engine</span> <span class="keyword">InnoDB</span> <span class="keyword">charset</span> utf8;</span><br></pre></td></tr></table></figure></p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">mysql&gt; insert into my_default(name) values('Harlon');</span><br><span class="line">Query OK, 1 row affected (0.00 sec)</span><br><span class="line"></span><br><span class="line">mysql&gt; insert into my_default values('Juice', 18, default);</span><br><span class="line">Query OK, 1 row affected (0.00 sec)</span><br><span class="line"></span><br><span class="line">mysql&gt; select * from my_default;</span><br><span class="line">+<span class="comment">--------+------+--------+</span></span><br><span class="line">| name   | age  | gender |</span><br><span class="line">+<span class="comment">--------+------+--------+</span></span><br><span class="line">| Harlon |    0 | 男     |</span><br><span class="line">| Juice  |   18 | 男     |</span><br><span class="line">+<span class="comment">--------+------+--------+</span></span><br><span class="line">2 rows in <span class="keyword">set</span> (<span class="number">0.00</span> sec)</span><br></pre></td></tr></table></figure>
<hr>
<h2 id="11-4-主键"><a href="#11-4-主键" class="headerlink" title="11.4 主键"></a>11.4 主键</h2><p>主键：primary key，表中主要的键，每张表只能有一个字段（复合主键，可以是多个字段）使用此属性，用来唯一的约束该字段里面的数据，不能重复。<br>增加主键：<br>在SQL操作中，有3种方法可以给表增加主键，分别为：<br>第1种：在创建表的时候，直接在字段之后，添加primary key关键字。  </p>
<p>实例：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"> <span class="keyword">create</span> <span class="keyword">table</span> my_pri (</span><br><span class="line">    <span class="keyword">name</span> <span class="built_in">varchar</span>(<span class="number">20</span>) <span class="keyword">not</span> <span class="literal">null</span> <span class="keyword">comment</span> <span class="string">'姓名'</span>,</span><br><span class="line">    <span class="built_in">number</span> <span class="built_in">char</span>(<span class="number">10</span>) primary <span class="keyword">key</span> <span class="keyword">comment</span> <span class="string">'学号'</span></span><br><span class="line">) <span class="keyword">engine</span> <span class="keyword">InnoDB</span> <span class="keyword">charset</span> utf8;</span><br></pre></td></tr></table></figure></p>
<p>这种方法清晰明了，缺点是只能使用一个字段作为主键。  </p>
<p>第2种：在创建表的时候，在所有的字段之后，使用primary key（主键字段列表）来创建主键（如果有多个字段作为主键，则称之为符合主键）  </p>
<p>实例：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">create</span> <span class="keyword">table</span> my_pri2(</span><br><span class="line">    <span class="built_in">number</span> <span class="built_in">char</span>(<span class="number">10</span>) <span class="keyword">not</span> <span class="literal">null</span> <span class="keyword">comment</span> <span class="string">'学号'</span>,</span><br><span class="line">    course <span class="built_in">char</span>(<span class="number">10</span>) <span class="keyword">not</span> <span class="literal">null</span> <span class="keyword">comment</span> <span class="string">'课程编号'</span>,</span><br><span class="line">    score <span class="built_in">tinyint</span> <span class="keyword">unsigned</span> <span class="keyword">default</span> <span class="number">60</span>,</span><br><span class="line">    primary <span class="keyword">key</span>(<span class="built_in">number</span>, course)</span><br><span class="line">) <span class="keyword">engine</span> <span class="keyword">InnoDB</span> <span class="keyword">charset</span> utf8;</span><br></pre></td></tr></table></figure></p>
<p>第3种：当表创建之后，额外追加主键，可以直接追加主键，也可以通过修改字段的属性追回主键。  </p>
<p>实例：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">create</span> <span class="keyword">table</span> my_pri3 (</span><br><span class="line">    course <span class="built_in">char</span>(<span class="number">10</span>) <span class="keyword">not</span> <span class="literal">null</span> <span class="keyword">comment</span> <span class="string">'课程编号'</span>,</span><br><span class="line">    <span class="keyword">name</span> <span class="built_in">varchar</span>(<span class="number">10</span>) <span class="keyword">not</span> <span class="literal">null</span> <span class="keyword">comment</span> <span class="string">'课程名称'</span></span><br><span class="line">) <span class="keyword">engine</span> <span class="keyword">InnoDB</span> <span class="keyword">charset</span> utf8;</span><br></pre></td></tr></table></figure></p>
<p>追加主键的方式有两种：</p>
<ul>
<li>alter table my_pri3 modify course char(10) primary key comment ‘课程’ – 不建议使用</li>
<li>alter table my_pri3 add primary key(course) – 推荐使用</li>
</ul>
<p>使用此方法的前提是表中的数据是不重复的，即保证唯一性。  </p>
<p>更新主键 &amp; 删除主键<br>对于主键，没有办法直接更新，主键必须先删除，然后才能更新<br>基本语法：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">alter</span> <span class="keyword">table</span> 表名 <span class="keyword">drop</span> primary <span class="keyword">key</span>;</span><br></pre></td></tr></table></figure></p>
<p>主键分类：<br>根据主键的字段类型，我们可以将主键分类两类，分别为：</p>
<ul>
<li>业务主键：即使用真实的业务数据作为主键，例如学号、课程编号等等，很少使用</li>
<li>逻辑主键：即使用逻辑性的字段作为主键，字段没有业务含义，值有没有都没有关系，经常使用</li>
</ul>
<hr>
<h2 id="11-5-自动增长"><a href="#11-5-自动增长" class="headerlink" title="11.5 自动增长"></a>11.5 自动增长</h2><p>自动增长：auto_increment，当对应的字段，不给值，或者是默认值，或者是null的时候，就会自动的被系统触发，系统会从当前字段中取得已有的最大值再进行+1操作，得到新的字段值。<br>自增长通过跟主键进行搭配使用，其特点是：</p>
<ul>
<li>任何字段要做自增长，前提其本身必须是一个索引，即key栏有值</li>
<li>自增必须是数字（整型）</li>
<li>每张表最多有一个自增长类型</li>
</ul>
<p>实例：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">create</span> <span class="keyword">table</span> my_auto (</span><br><span class="line">    <span class="keyword">id</span> <span class="built_in">int</span> primary <span class="keyword">key</span> auto_increment,</span><br><span class="line">    <span class="keyword">name</span> <span class="built_in">varchar</span>(<span class="number">20</span>) <span class="keyword">not</span> <span class="literal">null</span></span><br><span class="line">) <span class="keyword">engine</span> <span class="keyword">InnoDB</span> <span class="keyword">charset</span> utf8;</span><br></pre></td></tr></table></figure></p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line">mysql&gt; insert into my_auto values('Harlon');</span><br><span class="line">ERROR 1136 (21S01): Column count doesn't match value count at row 1</span><br><span class="line">mysql&gt; insert into my_auto(name) values('Harlon');      </span><br><span class="line">Query OK, 1 row affected (0.00 sec)</span><br><span class="line"></span><br><span class="line">mysql&gt; insert into my_auto values(null, 'Juice');</span><br><span class="line">Query OK, 1 row affected (0.30 sec)</span><br><span class="line"></span><br><span class="line">mysql&gt; insert into my_auto values(default, 'Ellie'); </span><br><span class="line">Query OK, 1 row affected (0.01 sec)</span><br><span class="line"></span><br><span class="line">mysql&gt; select * from my_auto;</span><br><span class="line">+<span class="comment">----+--------+</span></span><br><span class="line">| id | name   |</span><br><span class="line">+<span class="comment">----+--------+</span></span><br><span class="line">|  1 | Harlon |</span><br><span class="line">|  2 | Juice  |</span><br><span class="line">|  3 | Ellie  |</span><br><span class="line">+<span class="comment">----+--------+</span></span><br><span class="line">3 rows in <span class="keyword">set</span> (<span class="number">0.00</span> sec)</span><br><span class="line"></span><br><span class="line">mysql&gt; <span class="keyword">show</span> <span class="keyword">create</span> <span class="keyword">table</span> my_auto;</span><br><span class="line">+<span class="comment">---------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------+</span></span><br><span class="line">| Table   | <span class="keyword">Create</span> <span class="keyword">Table</span>                                                                                                                                                               |</span><br><span class="line">+<span class="comment">---------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------+</span></span><br><span class="line">| my_auto | <span class="keyword">CREATE</span> <span class="keyword">TABLE</span> <span class="string">`my_auto`</span> (</span><br><span class="line">  <span class="string">`id`</span> <span class="built_in">int</span>(<span class="number">11</span>) <span class="keyword">NOT</span> <span class="literal">NULL</span> AUTO_INCREMENT,</span><br><span class="line">  <span class="string">`name`</span> <span class="built_in">varchar</span>(<span class="number">20</span>) <span class="keyword">NOT</span> <span class="literal">NULL</span>,</span><br><span class="line">  PRIMARY <span class="keyword">KEY</span> (<span class="string">`id`</span>)</span><br><span class="line">) <span class="keyword">ENGINE</span>=<span class="keyword">InnoDB</span> AUTO_INCREMENT=<span class="number">4</span> <span class="keyword">DEFAULT</span> <span class="keyword">CHARSET</span>=utf8 |</span><br><span class="line">+<span class="comment">---------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------+</span></span><br><span class="line"><span class="number">1</span> <span class="keyword">row</span> <span class="keyword">in</span> <span class="keyword">set</span> (<span class="number">0.00</span> sec)</span><br></pre></td></tr></table></figure>
<p>修改自增长<br>自增长如果涉及到字段变化，必须先删除自增长，然后再增加增长，因为一个表只有有一个自增长字段。<br>如果修改当前自增长已经存在的值，则只能修改比当前已有自增长字段中的最大值更大，不能更小，因为更小不生效。<br>基本语法：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">alter</span> <span class="keyword">table</span> 表名 auto_increment = 值;</span><br></pre></td></tr></table></figure></p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">mysql&gt; show variables like 'auto_increment%';</span><br><span class="line">+<span class="comment">--------------------------+-------+</span></span><br><span class="line">| Variable_name            | Value |</span><br><span class="line">+<span class="comment">--------------------------+-------+</span></span><br><span class="line">| auto_increment_increment | 1     |</span><br><span class="line">| auto_increment_offset    | 1     |</span><br><span class="line">+<span class="comment">--------------------------+-------+</span></span><br><span class="line">2 rows in <span class="keyword">set</span> (<span class="number">0.00</span> sec)</span><br></pre></td></tr></table></figure>
<p>其中auto_increment_increment表示自增长步长，auto_increment_offset表示自增长的初值。  </p>
<p>删除自增长：<br>基本语法：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">alter</span> <span class="keyword">table</span> 表名 <span class="keyword">modify</span> 字段 类型</span><br></pre></td></tr></table></figure></p>
<p>实例：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">alter</span> <span class="keyword">table</span> my_auto <span class="keyword">modify</span> <span class="keyword">id</span> <span class="built_in">int</span>;</span><br></pre></td></tr></table></figure></p>
<hr>
<h2 id="11-6-唯一键"><a href="#11-6-唯一键" class="headerlink" title="11.6 唯一键"></a>11.6 唯一键</h2><p>唯一键：每张表往往有多个字段需要具有唯一性，数据不能重复，但是在每个表中，只能有一个主键，因此唯一键就是用来解决表中多个字段具有唯一性的问题。<br>唯一键的本质与主键差不多，唯一键默认的允许字段为空，而且可以多个字段为空，因此空字段不参与唯一性的比较。  </p>
<p>增加唯一性：<br>增加唯一性的方法与主键类型，有三种方法，分别为：<br>第一种：在创建表的时候，字段后面之间添加unique或者unique key关键字  </p>
<p>实例：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">create</span> <span class="keyword">table</span> my_unique (</span><br><span class="line">    <span class="built_in">number</span> <span class="built_in">char</span>(<span class="number">10</span>) <span class="keyword">unique</span> <span class="keyword">comment</span> <span class="string">'学号'</span>,</span><br><span class="line">    <span class="keyword">name</span> <span class="built_in">varchar</span>(<span class="number">20</span>) <span class="keyword">not</span> <span class="literal">null</span></span><br><span class="line">) <span class="keyword">engine</span> <span class="keyword">InnoDB</span> <span class="keyword">charset</span> utf8;</span><br></pre></td></tr></table></figure></p>
<p>第二种：在所有字段之后，添加unique key(字段列表)，可以设置复合唯一键。  </p>
<p>实例：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">create</span> <span class="keyword">table</span> my_unique2 (</span><br><span class="line">    <span class="built_in">number</span> <span class="built_in">char</span>(<span class="number">10</span>) <span class="keyword">not</span> <span class="literal">null</span>,</span><br><span class="line">    <span class="keyword">name</span> <span class="built_in">varchar</span>(<span class="number">10</span>) <span class="keyword">not</span> <span class="literal">null</span>,</span><br><span class="line">    <span class="keyword">unique</span> <span class="keyword">key</span>(<span class="built_in">number</span>)</span><br><span class="line">) <span class="keyword">engine</span> <span class="keyword">InnoDB</span> <span class="keyword">charset</span> utf8;</span><br></pre></td></tr></table></figure></p>
<p>当唯一键又非空时，就和主键的性质一样了。  </p>
<p>第3种：在创建表之后，添加唯一键。  </p>
<p>实例：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">create</span> <span class="keyword">table</span> my_unique3 (</span><br><span class="line">    <span class="keyword">id</span> <span class="built_in">int</span> primary <span class="keyword">key</span> auto_increment,</span><br><span class="line">    <span class="built_in">number</span> <span class="built_in">char</span>(<span class="number">10</span>) <span class="keyword">not</span> <span class="literal">null</span>,</span><br><span class="line">    <span class="keyword">name</span> <span class="built_in">varchar</span>(<span class="number">10</span>) <span class="keyword">not</span> <span class="literal">null</span>    </span><br><span class="line">) <span class="keyword">engine</span> <span class="keyword">InnoDB</span> <span class="keyword">charset</span> utf8;</span><br></pre></td></tr></table></figure></p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">alter</span> <span class="keyword">table</span> my_unique3 <span class="keyword">add</span> <span class="keyword">unique</span> <span class="keyword">key</span>(<span class="built_in">number</span>);</span><br></pre></td></tr></table></figure>
<p>唯一性约束：唯一键与主键本质上相同，区别在于：唯一键允许字段值为空，并且允许多个字段空值存在。  </p>
<p>更新唯一键 &amp; 删除唯一键<br>在表中，更新唯一键的时候，可以不用先删除唯一键，因为表的唯一键允许有多个。<br>删除唯一键的基本语法：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">alter</span> <span class="keyword">table</span> 表名 <span class="keyword">drop</span> <span class="keyword">index</span> 索引名字</span><br></pre></td></tr></table></figure></p>
<p>实例：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">alter</span> <span class="keyword">table</span> my_unique3 <span class="keyword">drop</span> <span class="keyword">index</span> <span class="built_in">number</span>;</span><br></pre></td></tr></table></figure></p>
<hr>
<h1 id="12、索引"><a href="#12、索引" class="headerlink" title="12、索引"></a>12、索引</h1><p>索引：系统根据某种算法，单独建立一个文件，根据这个文件能够快速的匹配数据，加快数据搜索。<br>索引的意义：</p>
<ul>
<li>提高查询数据的效率</li>
<li>约束数据的有效性</li>
</ul>
<p>MySQL中提供了多种索引，包括：</p>
<ul>
<li>主键索引（primary key）</li>
<li>唯一键索引（unique key）</li>
<li>全文索引（fulltext index）</li>
<li>普通索引（index）</li>
</ul>
<p>主键索引和唯一键索引，顾名思义就是在主键和唯一键字段上建立的索引，普通索引没有什么特色，就是为了加快数据查找。<br>全文索引，即根据文章内部的关键字进行索引，其最大的难度就是在于如何确定关键字。对于英文来说，全文索引的建立相对容易，因为英文的两个单词之间有空格；但是对于中文来说，全文索引的建立就比较难啦，因为中文两个字之间不仅没有空格，而是还可以随意组合。  </p>
<hr>
<h1 id="13、关系"><a href="#13、关系" class="headerlink" title="13、关系"></a>13、关系</h1><p>在数据库中，将实体与实体的关系反应到表的设计上来，可以分为三种，分别为：一对一（1：1），一对多（1：N）（或多对一（N：1））和多对多（N：N）。<br>在此，所有的关系都是指表与表之间的关系。  </p>
<h2 id="13-1-一对一"><a href="#13-1-一对一" class="headerlink" title="13.1 一对一"></a>13.1 一对一</h2><p>一对一，即一张表的一条记录只能与另一张表的一条记录想对应，反之亦然。  </p>
<p>实例：<br>设计一张「个人信息表」，其字段包括：姓名、性别、年龄、身高、体重、籍贯、居住地等。  </p>
<table>
<thead>
<tr>
<th>ID</th>
<th>姓名</th>
<th>性别</th>
<th>年龄</th>
<th>身高</th>
<th>体重</th>
<th>籍贯</th>
<th>居住地</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>Juice</td>
<td>男</td>
<td>18</td>
<td>182</td>
<td>72</td>
<td>中国</td>
<td>北京</td>
</tr>
<tr>
<td>2</td>
<td>Alice</td>
<td>女</td>
<td>18</td>
<td>170</td>
<td>52</td>
<td>美国</td>
<td>西雅图</td>
</tr>
</tbody>
</table>
<p>如上表所示，但是有些数据是我们经常使用的，有些不经常使用，每次查询的时候都要查询所有数据，影响效率，可以将常用的数据和不常用的数据分为两个表，例如：  </p>
<p>表1：常用数据</p>
<table>
<thead>
<tr>
<th>ID</th>
<th>姓名</th>
<th>性别</th>
<th>年龄</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>Juice</td>
<td>男</td>
<td>18</td>
</tr>
<tr>
<td>2</td>
<td>Alice</td>
<td>女</td>
<td>18</td>
</tr>
</tbody>
</table>
<p>表2：不常用数据</p>
<table>
<thead>
<tr>
<th>ID</th>
<th>身高</th>
<th>体重</th>
<th>籍贯</th>
<th>居住地</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>182</td>
<td>72</td>
<td>中国</td>
<td>北京</td>
</tr>
<tr>
<td>2</td>
<td>170</td>
<td>52</td>
<td>美国</td>
<td>西雅图</td>
</tr>
</tbody>
</table>
<p>如上面表2和表1所示，通过字段ID，表1中的一条记录只能匹配表2种的一条记录，反之亦然，这就是一对一的关系。  </p>
<hr>
<h2 id="13-2-一对多"><a href="#13-2-一对多" class="headerlink" title="13.2  一对多"></a>13.2  一对多</h2><p>一对多，即一张表中的记录可以对用另一张表中的多条记录，但是反过来，另一张表中的一条记录只能对应第一张表中的一条记录。  </p>
<p>实例：<br>例如，设计「国家城市表」，其中包含两个实体，即国家和城市。  </p>
<p>表3：国家表</p>
<table>
<thead>
<tr>
<th>COUNTRY_ID</th>
<th>国家</th>
<th>位置</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>中国</td>
<td>亚洲</td>
</tr>
<tr>
<td>2</td>
<td>美国</td>
<td>北美洲</td>
</tr>
</tbody>
</table>
<p>表4：城市表<br>| CITY_ID | 城市 | 国家 |<br>| — | — | — |<br>| 1 | 北京 | 中国 |<br>| 2 | 上海 | 中国 |<br>| 3 | 纽约 | 美国 |<br>| 4 | 华盛顿 | 美国 |</p>
<p>如上面表3和表4的关系，是一种典型的一对多的关系。</p>
<hr>
<h2 id="13-3-多对多"><a href="#13-3-多对多" class="headerlink" title="13.3 多对多"></a>13.3 多对多</h2><p>多对多，即一张表中的记录可以对应另一张表中的多条记录，反过来，另外一张表中的记录也可以对应第一张表中的多条记录。  </p>
<p>实例：<br>例如，设计「教师学生表」，其中包含两个实体，即学生和老师。  </p>
<p>表5：教师表</p>
<table>
<thead>
<tr>
<th>TEA_ID</th>
<th>姓名</th>
<th>性别</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>刘涛</td>
<td>女</td>
</tr>
<tr>
<td>2</td>
<td>胡歌</td>
<td>男</td>
</tr>
<tr>
<td>3</td>
<td>周杰伦</td>
<td>男</td>
</tr>
</tbody>
</table>
<p>表6：学生表</p>
<table>
<thead>
<tr>
<th>STU_ID</th>
<th>姓名</th>
<th>性别</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>张三</td>
<td>男</td>
</tr>
<tr>
<td>2</td>
<td>刘丽</td>
<td>女</td>
</tr>
</tbody>
</table>
<p>表7：中间表</p>
<table>
<thead>
<tr>
<th>ID</th>
<th>TEA_ID</th>
<th>STU_ID</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>1</td>
<td>1</td>
</tr>
<tr>
<td>2</td>
<td>1</td>
<td>2</td>
</tr>
<tr>
<td>3</td>
<td>2</td>
<td>1</td>
</tr>
<tr>
<td>3</td>
<td>3</td>
<td>2</td>
</tr>
</tbody>
</table>
<p>表5和表6之间是多对多的关系，他们的关系是通过表7来维护的。</p>
<hr>
<h1 id="14、范式"><a href="#14、范式" class="headerlink" title="14、范式"></a>14、范式</h1><p>范式：Normal Farmat，是为了解决数据存储和优化的问题。<br>在数据存储之后，凡是能通过关系寻找出来的数据，坚决不再重复存储，范式的终极目标是减少数据冗余。  </p>
<p>范式是一种分层结果的规范，共6层，分别为1NF、2NF、3NF、4NF、5NF和6NF，每一层都比上一层严格，若要满足下一层范式，其前提条件是满足上一层范式。其中，1NF是最底层的范式，6NF为最高层的范式，也最为严格。  </p>
<p>MySQL数据库属于关系型数据库，其存储数据的时候有些浪费空间，但也致力于节省空间，这就与范式想解决的问题不谋而合，因此在设计数据库的时候，大多利用范式来指导设计。但是数据库不单要解决存储空间的问题，还要保证效率的问题，而范式只为解决存储空间的问题，所以数据库的设计又不能完全按照范式的要求来实现，因此在一般情况下，只需要满足前三种范式即可。  </p>
<p>此外，咱们需要知道：范式在数据库的设计中是有指导意义的，但不是强制规范。  </p>
<h2 id="14-1-1NF"><a href="#14-1-1NF" class="headerlink" title="14.1 1NF"></a>14.1 1NF</h2><p>第一范式：在设计数据库的时候，如果表中设计的字段存储的数据，在取出来使用之前还需要额外的处理（拆分），那么表的设计就不满足第一范式，第一范式要求字段的数据具有原子性，不可再分。  </p>
<p>实例：<br>例如，设计一个「学校假期表」，如下所示：<br>表1：学校假期时间表  </p>
<table>
<thead>
<tr>
<th>ID</th>
<th>学校</th>
<th>起始时间，结束时间</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>清华大学</td>
<td>20180711, 20180901</td>
</tr>
<tr>
<td>2</td>
<td>华中科技大学</td>
<td>20180804, 20180820</td>
</tr>
</tbody>
</table>
<p>观察上表，咱们会发现表1的设计并没有什么问题，但是如果需求是查询各学校开始放假的日期呢？那显然上表的设计并不满足1NF，数据不具有原子性。对于此类问题，解决的方案就是将表1进行拆分：  </p>
<p>表2：拆分后的表1  </p>
<table>
<thead>
<tr>
<th>ID</th>
<th>学校</th>
<th>起始时间</th>
<th>结束时间</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>清华大学</td>
<td>20180711</td>
<td>20180901</td>
</tr>
<tr>
<td>2</td>
<td>华中科技大学</td>
<td>20180804</td>
<td>20180820</td>
</tr>
</tbody>
</table>
<hr>
<h2 id="14-2-2NF"><a href="#14-2-2NF" class="headerlink" title="14.2 2NF"></a>14.2 2NF</h2><p>第二范式：在数据表的设计过程中，如果有复合主键（多字段主键），且表中字段并不是由主键来确定，而是依赖复合主键的某个字段（主键的部分），也就是说存在依赖主键的部分依赖，第二范式就是解决表设计中不存在出现部分依赖。  </p>
<p>实例：<br>例如，设计一个「教室授课表」，如下所示：  </p>
<p>表3：教室授课表</p>
<table>
<thead>
<tr>
<th>教师</th>
<th>性别</th>
<th>课程</th>
<th>地点</th>
</tr>
</thead>
<tbody>
<tr>
<td>王伟</td>
<td>男</td>
<td>《如何追到心爱的女孩》</td>
<td>西12</td>
</tr>
<tr>
<td>李白</td>
<td>男</td>
<td>《唐诗三百首》</td>
<td>东9</td>
</tr>
</tbody>
</table>
<p>观察上表，我们发现：教室不能作为主键，需要与授课地点相结合才能成作为主键，其中性别依赖于具体的老师，而课程依赖于授课地点，这就出现了表的字段依赖于部分主键的问题，从而导致不能满足第二范式。</p>
<ul>
<li>解决方案1：将教师和性别，课程和授课地点，分成两张单独的表</li>
<li>解决方案2：取消复合主键，使用逻辑主键</li>
</ul>
<p>这里我们选择方案2。  </p>
<table>
<thead>
<tr>
<th>ID</th>
<th>教师</th>
<th>性别</th>
<th>课程</th>
<th>地点</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>王伟</td>
<td>男</td>
<td>《如何追到心爱的女孩》</td>
<td>西12</td>
</tr>
<tr>
<td>2</td>
<td>李白</td>
<td>男</td>
<td>《唐诗三百首》</td>
<td>东9</td>
</tr>
</tbody>
</table>
<hr>
<h2 id="14-3-3NF"><a href="#14-3-3NF" class="headerlink" title="14.3 3NF"></a>14.3 3NF</h2><p>第三范式：需要满足第一范式和第二范式，理论上讲，每张表中的所有字段都应该直接依赖主键（逻辑主键，代表是业务主键），如果表设计中存在一个字段，并不直接依赖主键，而是通过某个非主键字段依赖，最终实现主键依赖（把这种不是直接依赖主键，而是依赖非主键字段的依赖关系，称之为传递依赖），第三范式就是要解决表设计中出现传递依赖的问题。  </p>
<p>以上的添加逻辑主键的表3为例：  </p>
<table>
<thead>
<tr>
<th>ID</th>
<th>教师</th>
<th>性别</th>
<th>课程</th>
<th>地点</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>王伟</td>
<td>男</td>
<td>《如何追到心爱的女孩》</td>
<td>西12</td>
</tr>
<tr>
<td>2</td>
<td>李白</td>
<td>男</td>
<td>《唐诗三百首》</td>
<td>东9</td>
</tr>
</tbody>
</table>
<p>在以上表的设计中，性别依赖教师，教师依赖主键；课程依赖授课地点，授课地点依赖主键，因此性别和课程都存在传递依赖的问题。  </p>
<ul>
<li>解决方案：将存在传递依赖的字段，以及依赖的字段本身单独取出来，形成一个单独的表，然后在需要使用对应的信息的时候，把对应的实体表的主键添加进来。</li>
</ul>
<p>表4：教室表</p>
<table>
<thead>
<tr>
<th>TEA_ID</th>
<th>教师</th>
<th>性别</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>王伟</td>
<td>男</td>
</tr>
<tr>
<td>2</td>
<td>李白</td>
<td>男</td>
</tr>
</tbody>
</table>
<p>表5：授课地点表</p>
<table>
<thead>
<tr>
<th>ADDRESS_ID</th>
<th>课程</th>
<th>地点</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>《如何追到心爱的女孩》</td>
<td>西12</td>
</tr>
<tr>
<td>2</td>
<td>《唐诗三百首》</td>
<td>东9</td>
</tr>
</tbody>
</table>
<p>表6：进行处理后的表</p>
<table>
<thead>
<tr>
<th>ID</th>
<th>TEA_ID</th>
<th>ADDRESS_ID</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>1</td>
<td>1</td>
</tr>
<tr>
<td>2</td>
<td>2</td>
<td>2</td>
</tr>
<tr>
<td>3</td>
<td>3</td>
<td>3</td>
</tr>
</tbody>
</table>
<hr>
<h2 id="14-4-逆规范化"><a href="#14-4-逆规范化" class="headerlink" title="14.4 逆规范化"></a>14.4 逆规范化</h2><p>在某些特定的环境中（例如淘宝数据库），在设计表的时候，如果一张表中有几个字段是需要从另外的表中去获取数据，理论上讲，的确可以获得想要的数据，但是相对来说，其效率低会一点。此时为了提高查询效率，咱们会刻意的在某些表中，不去保存另外一张表的主键（逻辑主键），而是直接保存想要存储的数据信息，这样的话，在查询数据的时候，这张表就可以直接提供咱们想要的数据，而不需要多表查询，但是这样做会导致数据冗余。  </p>
<p>实际上，逆规范化是磁盘利用率和效率之间的对抗。</p>
<hr>
<h1 id="15、主键冲突"><a href="#15、主键冲突" class="headerlink" title="15、主键冲突"></a>15、主键冲突</h1><p>数据的操作，无外乎就是增删改查。<br>在插入数据的时候，假设主键对用的值已经存在，则插入失败，这就是主键冲突。  </p>
<p>第一种情况：主键冲突，进行更新操作<br>基本语法：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> 表名[(字段列表：包含主键)]  <span class="keyword">values</span>(值列表) <span class="keyword">on</span> <span class="keyword">duplicate</span>  <span class="keyword">key</span> <span class="keyword">update</span> 字段 = 新值;</span><br></pre></td></tr></table></figure></p>
<p>实例：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> my_class <span class="keyword">values</span>(<span class="string">'PM'</span>, <span class="string">'B313'</span>) <span class="keyword">on</span> <span class="keyword">duplicate</span> <span class="keyword">key</span> <span class="keyword">update</span> room = <span class="string">'B313'</span></span><br></pre></td></tr></table></figure></p>
<p>第二种情况：主键冲突，选择替换操作<br>基本语法：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">replace</span> <span class="keyword">insert</span> <span class="keyword">into</span> 表名[(字段列表：包含主键)]  <span class="keyword">values</span>(值列表);</span><br></pre></td></tr></table></figure></p>
<p>实例：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> my_class <span class="keyword">values</span>(<span class="string">'PM'</span>, <span class="string">'B313'</span>);</span><br><span class="line"><span class="keyword">replace</span> <span class="keyword">into</span> my_class <span class="keyword">values</span>(<span class="string">'PM'</span>, <span class="string">'B313'</span>);</span><br></pre></td></tr></table></figure></p>
<hr>
<h1 id="16、蠕虫复制"><a href="#16、蠕虫复制" class="headerlink" title="16、蠕虫复制"></a>16、蠕虫复制</h1><p>蠕虫复制：从已有的表中获取数据，然后将数据进行新增操作，数据成倍（以指数形式）的增加。<br>根据已有表创建表，即复制表结构，其基本语法为：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">create</span> <span class="keyword">table</span> 表名 <span class="keyword">like</span> [数据库名.]表名;</span><br></pre></td></tr></table></figure></p>
<p>实例：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">mysql&gt; create table my_copy like my_auto;</span><br><span class="line">Query OK, 0 rows affected (0.01 sec)</span><br><span class="line"></span><br><span class="line">mysql&gt; desc my_copy;</span><br><span class="line">+<span class="comment">-------+-------------+------+-----+---------+----------------+</span></span><br><span class="line">| Field | Type        | Null | Key | Default | Extra          |</span><br><span class="line">+<span class="comment">-------+-------------+------+-----+---------+----------------+</span></span><br><span class="line">| id    | int(11)     | NO   | PRI | NULL    | auto_increment |</span><br><span class="line">| name  | varchar(20) | NO   |     | NULL    |                |</span><br><span class="line">+<span class="comment">-------+-------------+------+-----+---------+----------------+</span></span><br><span class="line">2 rows in <span class="keyword">set</span> (<span class="number">0.00</span> sec)</span><br></pre></td></tr></table></figure></p>
<p>蠕虫复制的步骤为：先查出数据，然后将查出的数据新增一遍。<br>基本语法：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> 表名 <span class="keyword">select</span> 字段列表<span class="comment">/* from 表名;</span></span><br></pre></td></tr></table></figure></p>
<p>实例：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">mysql&gt; insert into my_copy select * from my_auto;</span><br><span class="line">Query OK, 3 rows affected (0.00 sec)</span><br><span class="line">Records: 3  Duplicates: 0  Warnings: 0</span><br><span class="line"></span><br><span class="line">mysql&gt; select * from my_copy;</span><br><span class="line">+<span class="comment">----+--------+</span></span><br><span class="line">| id | name   |</span><br><span class="line">+<span class="comment">----+--------+</span></span><br><span class="line">|  1 | Harlon |</span><br><span class="line">|  2 | Juice  |</span><br><span class="line">|  3 | Ellie  |</span><br><span class="line">+<span class="comment">----+--------+</span></span><br><span class="line">3 rows in <span class="keyword">set</span> (<span class="number">0.00</span> sec)</span><br></pre></td></tr></table></figure></p>
<p>蠕虫复制的效果：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">mysql&gt; insert into my_copy(name) select name from my_copy;</span><br><span class="line">Query OK, 3 rows affected (0.01 sec)</span><br><span class="line">Records: 3  Duplicates: 0  Warnings: 0</span><br><span class="line"></span><br><span class="line">mysql&gt; insert into my_copy(name) select name from my_copy;</span><br><span class="line">Query OK, 6 rows affected (0.00 sec)</span><br><span class="line">Records: 6  Duplicates: 0  Warnings: 0</span><br><span class="line"></span><br><span class="line">mysql&gt; insert into my_copy(name) select name from my_copy;</span><br><span class="line">Query OK, 12 rows affected (0.00 sec)</span><br><span class="line">Records: 12  Duplicates: 0  Warnings: 0</span><br><span class="line"></span><br><span class="line">mysql&gt; insert into my_copy(name) select name from my_copy;</span><br><span class="line">Query OK, 24 rows affected (0.01 sec)</span><br><span class="line">Records: 24  Duplicates: 0  Warnings: 0</span><br></pre></td></tr></table></figure></p>
<p>蠕虫复制的意义：</p>
<ul>
<li>从已有的数据表中拷贝数据到新的数据表</li>
<li>可以迅速的让表中的数据膨胀到一定的数量级，多用于测试表的压力及效率</li>
</ul>
<hr>
<h1 id="17、数据库高级操作"><a href="#17、数据库高级操作" class="headerlink" title="17、数据库高级操作"></a>17、数据库高级操作</h1><h2 id="17-1-更新数据"><a href="#17-1-更新数据" class="headerlink" title="17.1 更新数据"></a>17.1 更新数据</h2><p>基本语法：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">update</span> 表名 <span class="keyword">set</span> 字段 = 值 [<span class="keyword">where</span> 条件]</span><br></pre></td></tr></table></figure></p>
<p>高级语法：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">update</span> 表名 <span class="keyword">set</span> 字段 = 值 [<span class="keyword">where</span> 条件] [<span class="keyword">limit</span> 更新数量]</span><br></pre></td></tr></table></figure></p>
<p>实例：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">mysql&gt; update my_copy set name = 'Harlon2' where name = 'harlon' limit 3;</span><br><span class="line">Query OK, 3 rows affected (0.00 sec)</span><br><span class="line">Rows matched: 3  Changed: 3  Warnings: 0</span><br><span class="line"></span><br><span class="line">mysql&gt; select * from my_copy;</span><br><span class="line">+<span class="comment">----+---------+</span></span><br><span class="line">| id | name    |</span><br><span class="line">+<span class="comment">----+---------+</span></span><br><span class="line">|  1 | Harlon2 |</span><br><span class="line">|  2 | Juice   |</span><br><span class="line">|  3 | Ellie   |</span><br><span class="line">|  4 | Harlon2 |</span><br><span class="line">|  5 | Juice   |</span><br><span class="line">|  6 | Ellie   |</span><br><span class="line">|  7 | Harlon2 |</span><br><span class="line">...</span><br></pre></td></tr></table></figure></p>
<hr>
<h2 id="17-2-删除数据"><a href="#17-2-删除数据" class="headerlink" title="17.2 删除数据"></a>17.2 删除数据</h2><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">delete</span> <span class="keyword">from</span> 表名 [<span class="keyword">where</span> 条件]</span><br></pre></td></tr></table></figure>
<p>高级语法：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">delete</span> <span class="keyword">from</span> 表名 [<span class="keyword">where</span> 条件] [<span class="keyword">limit</span> 更新数量]</span><br></pre></td></tr></table></figure></p>
<p>实例：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">mysql&gt; delete from my_copy where name = 'Harlon2' limit 2;</span><br><span class="line">Query OK, 2 rows affected (0.00 sec)</span><br><span class="line"></span><br><span class="line">mysql&gt; select * from my_copy;</span><br><span class="line">+<span class="comment">----+---------+</span></span><br><span class="line">| id | name    |</span><br><span class="line">+<span class="comment">----+---------+</span></span><br><span class="line">|  2 | Juice   |</span><br><span class="line">|  3 | Ellie   |</span><br><span class="line">|  5 | Juice   |</span><br><span class="line">|  6 | Ellie   |</span><br><span class="line">|  7 | Harlon2 |</span><br><span class="line">|  8 | Juice   |</span><br><span class="line">...</span><br></pre></td></tr></table></figure></p>
<p>删除表的数据，自增长不会还原，如果想要还原自增长属性，思路是：先删除表，然后重新建表。<br>基本语法：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">truncate</span> 表名;</span><br></pre></td></tr></table></figure></p>
<hr>
<h1 id="18、查询高级操作"><a href="#18、查询高级操作" class="headerlink" title="18、查询高级操作"></a>18、查询高级操作</h1><p>基本语法：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> 字段列表<span class="comment">/* from 表名 [where 条件]</span></span><br></pre></td></tr></table></figure></p>
<p>完整语法：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> [<span class="keyword">select</span> 选项] 字段列表[字段别名]<span class="comment">/* from 数据源 [where条件]</span></span><br><span class="line"><span class="comment">[1] [2] [3]</span></span><br></pre></td></tr></table></figure></p>
<ul>
<li>[1] = [group by 子句]</li>
<li>[2] = [order by 子句]</li>
<li>[3] = [limit 子句]</li>
</ul>
<hr>
<h2 id="18-1-select-选项"><a href="#18-1-select-选项" class="headerlink" title="18.1 select 选项"></a>18.1 select 选项</h2><p>select选项，即select对查出来的结果的处理方式。</p>
<ul>
<li>all：默认，保留所有的查询结果</li>
<li>distinct：去重，将查出来的结果中所有字段相同的记录去除</li>
</ul>
<p>实例：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> * <span class="keyword">from</span> my_copy;</span><br><span class="line"><span class="keyword">select</span> <span class="keyword">all</span> * <span class="keyword">from</span> my_copy;</span><br><span class="line"><span class="keyword">select</span> <span class="keyword">distinct</span> * <span class="keyword">from</span> my_copy;</span><br></pre></td></tr></table></figure></p>
<hr>
<h2 id="18-2-字段别名"><a href="#18-2-字段别名" class="headerlink" title="18.2 字段别名"></a>18.2 字段别名</h2><p>字段别名，即当数据进行查询的时候，有时候字段的名字不一定满足需求（特别地，在多表查询的时候，很有可能会有同名字段），这就需要对字段进行重命名、取别名。<br>基本语法：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">字段名 as 别名;</span><br></pre></td></tr></table></figure></p>
<p>实例：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> <span class="keyword">id</span> <span class="keyword">name</span> <span class="keyword">as</span> n age <span class="keyword">as</span> a grade <span class="keyword">as</span> g <span class="keyword">from</span> student;</span><br></pre></td></tr></table></figure></p>
<hr>
<h2 id="18-3-数据源"><a href="#18-3-数据源" class="headerlink" title="18.3 数据源"></a>18.3 数据源</h2><p>数据源，即数据的来源，关系型数据库的数据源都是数据表，本质上只要保证数据类型二维表，最终就可以做为数据源。<br>数据源分为3种，分别为：单表数据源，多表数据源和查询语句。<br>第1种：单数据源<br>基本语法：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> * <span class="keyword">from</span> 表名;</span><br></pre></td></tr></table></figure></p>
<p>第2种：多数据源<br>基本语法：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> * <span class="keyword">from</span> 表名<span class="number">1</span>, 表名<span class="number">2</span></span><br></pre></td></tr></table></figure></p>
<p>第3种：查询语句（子查询）<br>基本语法：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> * <span class="keyword">from</span> (<span class="keyword">select</span> * <span class="keyword">from</span> 表名) [<span class="keyword">as</span>] 别名;</span><br></pre></td></tr></table></figure></p>
<hr>
<h2 id="18-4-where子句"><a href="#18-4-where子句" class="headerlink" title="18.4 where子句"></a>18.4 where子句</h2><p>where子句：用来判断数据和筛选数据，返回的结果为0或者1，其中0代表false，1代表true，where是唯一一个直接从磁盘获取数据的时候就开始判断的条件，从磁盘中读取一条数据，就开始where判断，如果判断的结果为真，则保持，反之，不保存。<br>判断条件：</p>
<ul>
<li>比较运算符：&gt;、&lt;、&gt;=、&lt;=、&lt;&gt;、=、like、between and、in和not in</li>
<li>逻辑运算符：&amp;&amp;、||、和!</li>
</ul>
<p>实例：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> * <span class="keyword">from</span> student <span class="keyword">where</span> <span class="keyword">id</span> = <span class="number">2</span> || <span class="keyword">id</span> = <span class="number">3</span> || <span class="keyword">id</span> = <span class="number">5</span>;</span><br><span class="line"><span class="keyword">select</span> * <span class="keyword">from</span> student <span class="keyword">where</span> <span class="keyword">id</span> <span class="keyword">in</span> (<span class="number">2</span>,<span class="number">3</span>,<span class="number">5</span>);</span><br><span class="line"><span class="keyword">select</span> * <span class="keyword">from</span> student <span class="keyword">where</span> <span class="keyword">if</span> <span class="keyword">between</span> <span class="number">2</span> <span class="keyword">and</span> <span class="number">5</span>;</span><br></pre></td></tr></table></figure></p>
<hr>
<h2 id="18-5-group-by-子句"><a href="#18-5-group-by-子句" class="headerlink" title="18.5 group by 子句"></a>18.5 group by 子句</h2><p>group by 子句：根据表中的某个字段进行分组，即将含有相同字段值的记录放在一组，不同的放在不同组。<br>基本语法：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">group by 字段名;</span><br></pre></td></tr></table></figure></p>
<p>实例：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> * <span class="keyword">from</span> student <span class="keyword">group</span> <span class="keyword">by</span> sex;</span><br></pre></td></tr></table></figure></p>
<p>分组的目标是为了（按分组字段）统计数据，并不是为了单纯为了方便统计数据，SQL提供了一些列的统计函数，例如：</p>
<ul>
<li>count()：统计分组后，每组的记录数；</li>
<li>max()：统计每组中的最大值；</li>
<li>min()：统计每组中的最小值；</li>
<li>avg()：统计每组中的平均值；</li>
<li>sum()：统计每组中的数据总和；</li>
</ul>
<p>实例：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> sex,<span class="keyword">count</span>(*),<span class="keyword">max</span>(age),<span class="keyword">min</span>(age),<span class="keyword">avg</span>(age),<span class="keyword">sum</span>(age) <span class="keyword">from</span> student <span class="keyword">group</span> <span class="keyword">by</span> sex;</span><br></pre></td></tr></table></figure></p>
<p>其中，count()函数可以使用两种参数，分为为：*表示统计组内全部记录的数量；字段名表示统计对应字段的非null（如果某条记录中该字段的值为null，则不统计）记录的总数。此外，使用group by进行分组之后，展示的记录会根据分组的字段值进行排序，默认为升序。当然，也可以人为的设置升序和降序。<br>基本语法：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">group by 字段名 [asc/desc]</span><br></pre></td></tr></table></figure></p>
<p>实例：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> sex, <span class="keyword">count</span>(*) <span class="keyword">from</span> student <span class="keyword">group</span> <span class="keyword">by</span> sex;</span><br><span class="line"><span class="keyword">select</span> sex, <span class="keyword">count</span>(*) <span class="keyword">from</span> student <span class="keyword">group</span> <span class="keyword">by</span> sex <span class="keyword">asc</span>;</span><br><span class="line"><span class="keyword">select</span> sex, <span class="keyword">count</span>(*) <span class="keyword">from</span> student <span class="keyword">group</span> <span class="keyword">by</span> sex <span class="keyword">desc</span>;</span><br></pre></td></tr></table></figure></p>
<p>多字段分组：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> *, <span class="keyword">count</span>(*) <span class="keyword">from</span> student <span class="keyword">group</span> <span class="keyword">by</span> grage, sex;</span><br></pre></td></tr></table></figure></p>
<p>group_concat(字段名)可以对分组的结果中的某个字段值进行字符串链接，即保留该组某个字段的所有值，例如：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> sex, age, <span class="keyword">count</span>(*), <span class="keyword">group_concat</span>(<span class="keyword">name</span>) <span class="keyword">from</span> student <span class="keyword">group</span> <span class="keyword">by</span> sex;</span><br></pre></td></tr></table></figure></p>
<p>回溯统计：利用with rollup关键字，可以在每次分组之后，根据当前分组的字段进行统计，并向上一级分组进行汇报，例如：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> sex, <span class="keyword">count</span>(*) <span class="keyword">from</span> student <span class="keyword">group</span> <span class="keyword">by</span> sex <span class="keyword">with</span> <span class="keyword">rollup</span>;</span><br></pre></td></tr></table></figure></p>
<hr>
<h2 id="18-6-having-子句"><a href="#18-6-having-子句" class="headerlink" title="18.6 having 子句"></a>18.6 having 子句</h2><p>having子句：与where子句一样，都是进行条件判断的，但是where是针对磁盘数据进入内存之后，会进行分组操作，分组结果就需要having来操作。思考可以，having能做where能做的几乎所有事情，但是where却不能做having能做的很多事情。<br>第1点：分组统计的结果或者说统计函数只有having能够使用</p>
<p>实例：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> grage, <span class="keyword">count</span>(*) <span class="keyword">from</span> student <span class="keyword">group</span> <span class="keyword">by</span> grade <span class="keyword">having</span> <span class="keyword">count</span>(*) &gt;= <span class="number">2</span>;</span><br></pre></td></tr></table></figure></p>
<p>第2点：having能够使用字段别名，where则不能</p>
<p>实例：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> grage, <span class="keyword">count</span>(*) <span class="keyword">as</span> total <span class="keyword">from</span> student <span class="keyword">group</span> <span class="keyword">by</span> grade <span class="keyword">having</span> total &gt;= <span class="number">2</span>;</span><br></pre></td></tr></table></figure></p>
<hr>
<h2 id="18-7-order-by子句"><a href="#18-7-order-by子句" class="headerlink" title="18.7 order by子句"></a>18.7 order by子句</h2><p>order by子句：根据某个字段进行升序或者降序排序，依赖校对集。<br>基本语法：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">order by 字段 asc/desc</span><br></pre></td></tr></table></figure></p>
<p>其中，asc为升序，desc为降序。</p>
<p>实例：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> * <span class="keyword">from</span> student <span class="keyword">order</span> <span class="keyword">by</span> age;</span><br></pre></td></tr></table></figure></p>
<p>多字段排序，即先根据某个字段进行排序，然后在排序后的结果中，再根据某个字段进行排序。  </p>
<p>实例：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> * <span class="keyword">from</span> student <span class="keyword">order</span> <span class="keyword">by</span> age, grade <span class="keyword">desc</span>;</span><br></pre></td></tr></table></figure></p>
<hr>
<p>18.8 limit子句<br>limit子句：是一种限制结果的语句，通常来限制结果的数量。<br>基本语法：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">limit [offset] length;</span><br></pre></td></tr></table></figure></p>
<p>其中，offset为起始值，length为长度。<br>第1种，只用来限制长度（数据量）<br>实例：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> * <span class="keyword">from</span> student;</span><br><span class="line"><span class="keyword">select</span> * <span class="keyword">from</span> student <span class="keyword">limit</span> <span class="number">3</span>;</span><br></pre></td></tr></table></figure></p>
<p>第2种：限制起始值，限制长度（数据量）<br>实例：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> * <span class="keyword">from</span> student <span class="keyword">limit</span> <span class="number">0</span>,<span class="number">2</span></span><br><span class="line"><span class="keyword">select</span> * <span class="keyword">from</span> student <span class="keyword">limit</span> <span class="number">2</span>,<span class="number">2</span></span><br></pre></td></tr></table></figure></p>
<p>第3种：主要用来实现数据的分页，目的是为了节省时间，提高服务器的响应效率，减少资源的浪费。  </p>
<hr>
<h1 id="19、连接查询"><a href="#19、连接查询" class="headerlink" title="19、连接查询"></a>19、连接查询</h1><p>连接查询：将多张表（大于等于2张表）按照某个指定的条件进行数据的拼接，其最终结果记录数可能有变化，但字段一定会增加。<br>连接查询的意义：在用户查询的时候，需要显示的数据来自多张表。<br>连接查询为join，使用方式为：左表 join 右表  </p>
<ul>
<li>左表：join 左表的表</li>
<li>右表：join 右边的表</li>
</ul>
<p>连接查询分类：在SQL中将连接查询分为四类：分别为内连接、外链接、自然连接和交叉连接。  </p>
<h2 id="19-1-交叉连接"><a href="#19-1-交叉连接" class="headerlink" title="19.1 交叉连接"></a>19.1 交叉连接</h2><p>交叉连接：cross join，从一张表中循环取出每条记录，每条记录都去另外一张表进行匹配，匹配的结果都保留（没有条件匹配），而连接本身的字段会增加，最终形成的结果为笛卡尔积形式。<br>基本语法：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">左表 cross join 右表;</span><br></pre></td></tr></table></figure></p>
<p>其结果与多表查询相同。<br>实例：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> * <span class="keyword">from</span> student <span class="keyword">cross</span> <span class="keyword">join</span> <span class="keyword">class</span>;</span><br><span class="line"><span class="keyword">select</span> * <span class="keyword">from</span> student, <span class="keyword">class</span>;</span><br></pre></td></tr></table></figure></p>
<p>实际上，笛卡尔积形式（交叉连接和多表查询）的结果并没有什么实际意义，应该尽量避免，其存在的价值就是保证连接这种结构的完整性。</p>
<hr>
<h2 id="19-2-内连接"><a href="#19-2-内连接" class="headerlink" title="19.2 内连接"></a>19.2 内连接</h2><p>内连接：inner join，从左表中取出每一条记录，和右表中的所有记录进行匹配，仅当某个条件在左表和右表的值相同时，结果才会保留，否则不保留。<br>基本语法：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">左表 [inner] join 右表 on 左表.字段 = 右表.字段;</span><br></pre></td></tr></table></figure></p>
<p>其中，关键字on表示连接条件，两表中的条件字段有着相同的业务含义。<br>实例：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> * <span class="keyword">from</span> student <span class="keyword">inner</span> <span class="keyword">join</span> <span class="keyword">class</span> <span class="keyword">on</span> student.grade = class.grade;</span><br><span class="line"><span class="keyword">select</span> * <span class="keyword">from</span> student  <span class="keyword">join</span> <span class="keyword">class</span> <span class="keyword">on</span> student.grade = class.grade;</span><br></pre></td></tr></table></figure></p>
<p>注意：<br>如果两表中有某个表的条件字段名唯一，那么在书写连接条件的时候，可以省略表名，直接书写字段名，MySQL 会自动识别唯一字段名，但不建议这么做。此外，咱们会发现，在上面的结果中有同名字段，这会给咱们理解数据的意义造成一定的困扰，这时就需要使用字段别名和表别名做区别啦！<br>实例：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> s.*, c.id <span class="keyword">as</span> c_id, c.grade <span class="keyword">as</span> c_grade, room <span class="keyword">from</span> student <span class="keyword">as</span> s <span class="keyword">inner</span> <span class="keyword">join</span> <span class="keyword">class</span> <span class="keyword">as</span> c <span class="keyword">on</span> s.grade = c.grade;</span><br></pre></td></tr></table></figure></p>
<p>最后，内连接可以可以没有连接条件，即可以没有on及之后的内容，这时内连接的结果全部保留，与交叉连接的结果完全相同。而且在内连接的时候可以使用where关键字代替on，但不建议这么做，因为where没有on的效率高。</p>
<hr>
<h2 id="19-3-外链接"><a href="#19-3-外链接" class="headerlink" title="19.3 外链接"></a>19.3 外链接</h2><p>外链接：<code>left\right join</code>，以某张表为主表，取出里面的所有记录，然后让主表中的每条记录都与另外一张表进行连接，不管是否匹配成功，其最终结果都会保留，匹配成功，则正确保留；匹配失败，则将另一张表的字段都置为<code>NULL</code>。<br>基本语法：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">左表 left\right join 右表 on 左表.字段 = 右表.字段</span><br></pre></td></tr></table></figure></p>
<p>其中，<code>on</code>表示连接条件，两表的字段有着相同的含义。在这里，以主表为依据，外连接分为两种，分别为：</p>
<ul>
<li><code>left join</code>：左外连接（左连接），以左表为主表；</li>
<li><code>right join</code>：右外连接（右连接），以右表为主表；</li>
</ul>
<p>实例：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> s.*, c.id <span class="keyword">as</span> c_id, c.grade <span class="keyword">as</span> c_grade, room <span class="keyword">from</span> student <span class="keyword">as</span> s <span class="keyword">left</span> <span class="keyword">join</span> <span class="keyword">class</span> <span class="keyword">as</span> c <span class="keyword">on</span> s.grade = c.grade;</span><br><span class="line"><span class="keyword">select</span> s.*, c.id sd c_id, c.grade <span class="keyword">as</span> c_grade, room <span class="keyword">from</span> student <span class="keyword">as</span> s <span class="keyword">right</span> <span class="keyword">join</span> <span class="keyword">class</span> <span class="keyword">as</span> c <span class="keyword">on</span> s.grade = c.grade;</span><br></pre></td></tr></table></figure></p>
<p>实际上，无论以那张表为主表，其外连接的结果（记录数量）都不会少于主表的记录总数。此外，虽然左连接与右连接有主表差异，但显示的结果都是：左表的数据在左边，右表的数据在右边。</p>
<hr>
<h2 id="19-4-自然连接"><a href="#19-4-自然连接" class="headerlink" title="19.4 自然连接"></a>19.4 自然连接</h2><p>自然连接：<code>nature join</code>，自然连接其实就是自动匹配连接条件，系统以两表中同名字段作为匹配条件，如果两表有多个同名字段，那就都作为匹配条件。在这里，自然连接可以分为自然内连接和自然外连接。  </p>
<p>自然内连接：<br>基本语法：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">左表 + natural + join + 右表;</span><br></pre></td></tr></table></figure></p>
<p>实例：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> * <span class="keyword">from</span> student <span class="keyword">natural</span> <span class="keyword">join</span> <span class="keyword">class</span>;</span><br></pre></td></tr></table></figure></p>
<p>自然连接自动使用同名字段作为连接条件，而且在连接完成之后合并同名字段。  </p>
<p>自然外连接：<br>基本语法：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">左表 natural left/right join 右表</span><br></pre></td></tr></table></figure></p>
<p>实例：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> * <span class="keyword">from</span> student <span class="keyword">natural</span> <span class="keyword">left</span> <span class="keyword">join</span> <span class="keyword">class</span>;</span><br><span class="line"><span class="keyword">select</span> * <span class="keyword">from</span> student <span class="keyword">natural</span> <span class="keyword">right</span> <span class="keyword">join</span> <span class="keyword">class</span>;</span><br></pre></td></tr></table></figure></p>
<p>实际上，自然连接并不常用。而且，咱们可以用内连接和外连接来模拟自然连接，模拟的关键就在于使用同名字段作为连接条件及合并同名字段。<br>基本语法：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">左表 inner/left/right join 右表 using(字段名);</span><br></pre></td></tr></table></figure></p>
<p>实例：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> * <span class="keyword">from</span> student <span class="keyword">natural</span> <span class="keyword">left</span> <span class="keyword">join</span> <span class="keyword">class</span>;</span><br><span class="line"><span class="keyword">select</span> * <span class="keyword">from</span> student <span class="keyword">left</span> <span class="keyword">join</span> <span class="keyword">class</span> <span class="keyword">using</span>(<span class="keyword">id</span>, grade);</span><br></pre></td></tr></table></figure></p>
<hr>
<h1 id="20、外键"><a href="#20、外键" class="headerlink" title="20、外键"></a>20、外键</h1><p>外键：<code>foreign key</code>，外面的键，即不在自己表中的键。如果一张表中的有一个非主键的字段指向另外一张表的主键，那么该字段称之为主键。每张表中，可有有多个主键。  </p>
<h2 id="20-1-外键操作"><a href="#20-1-外键操作" class="headerlink" title="20.1 外键操作"></a>20.1 外键操作</h2><p>新增主键：<br>主键即可以再创建表的时候增加，也可以在创建之后增加（但是要考虑数据的问题）。<br>第1种：在创建表的时候，增加外键。<br>基本语法：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">foreign key(外键字段) references 外部表名(主键字段);</span><br></pre></td></tr></table></figure></p>
<p>实例：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">create</span> <span class="keyword">table</span> my_foreign1(</span><br><span class="line">    <span class="keyword">id</span> <span class="built_in">int</span> primary <span class="keyword">key</span> auto_increment,</span><br><span class="line">    <span class="keyword">name</span> <span class="built_in">varchar</span>(<span class="number">20</span>) <span class="keyword">not</span> <span class="literal">null</span> <span class="keyword">comment</span> <span class="string">'学生姓名'</span>,</span><br><span class="line">    c_id <span class="built_in">int</span> <span class="keyword">comment</span> <span class="string">'班级表ID'</span>,</span><br><span class="line">    <span class="keyword">foreign</span> <span class="keyword">key</span>(c_id) <span class="keyword">references</span> my_class(<span class="keyword">id</span>)</span><br><span class="line">) <span class="keyword">engine</span> <span class="keyword">InnoDB</span> <span class="keyword">charset</span> utf8;</span><br></pre></td></tr></table></figure></p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">mysql&gt; desc my_foreign1;</span><br><span class="line">+<span class="comment">-------+-------------+------+-----+---------+----------------+</span></span><br><span class="line">| Field | Type        | Null | Key | Default | Extra          |</span><br><span class="line">+<span class="comment">-------+-------------+------+-----+---------+----------------+</span></span><br><span class="line">| id    | int(11)     | NO   | PRI | NULL    | auto_increment |</span><br><span class="line">| name  | varchar(20) | NO   |     | NULL    |                |</span><br><span class="line">| c_id  | int(11)     | YES  | MUL | NULL    |                |</span><br><span class="line">+<span class="comment">-------+-------------+------+-----+---------+----------------+</span></span><br><span class="line">3 rows in <span class="keyword">set</span> (<span class="number">0.00</span> sec)</span><br></pre></td></tr></table></figure>
<p><code>c_id</code>的<code>key</code>显示为<code>MUL</code>，表示多个键的意思。这是因为外键要求字段本身是一个索引（普通索引）如果字段本身没有索引，外键就会先创建一个索引，然后才创建外键本身。  </p>
<p>第2中：在创建表之后，增加外键<br>基本语法：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">alter</span> <span class="keyword">table</span> 表名 <span class="keyword">add</span>[<span class="keyword">constraint</span> 外键名字] <span class="keyword">foreign</span> <span class="keyword">key</span>(外键字段) + <span class="keyword">references</span> 外键表名(主键字段);</span><br></pre></td></tr></table></figure></p>
<p>实例：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">create</span> <span class="keyword">table</span> my_foreign2 (</span><br><span class="line">    <span class="keyword">id</span> <span class="built_in">int</span> primary <span class="keyword">key</span> auto_increment,</span><br><span class="line">    <span class="keyword">name</span> <span class="built_in">varchar</span>(<span class="number">20</span>) <span class="keyword">not</span> <span class="literal">null</span> <span class="keyword">comment</span> <span class="string">'学生姓名'</span>,</span><br><span class="line">    c_id <span class="built_in">int</span> <span class="keyword">comment</span> <span class="string">'班级表ID'</span></span><br><span class="line">) <span class="keyword">engine</span> <span class="keyword">InnoDB</span> <span class="keyword">charset</span> utf8;</span><br><span class="line"></span><br><span class="line"><span class="keyword">alter</span> <span class="keyword">table</span> my_foreign <span class="keyword">add</span> <span class="keyword">constraint</span> test_foreign <span class="keyword">foreign</span> <span class="keyword">key</span>(c_id) <span class="keyword">references</span> my_class(<span class="keyword">id</span>);</span><br></pre></td></tr></table></figure></p>
<p>修改外键 &amp; 删除外键<br>外键不能修改，只能先删除后增加。<br>基本语法：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">alter</span> <span class="keyword">table</span> 表名 <span class="keyword">drop</span> <span class="keyword">foreign</span> <span class="keyword">key</span> 外键名字;</span><br></pre></td></tr></table></figure></p>
<p>实例：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">alter</span> <span class="keyword">table</span> my_foreign2 <span class="keyword">drop</span> <span class="keyword">foreign</span> <span class="keyword">key</span> test_foreign;</span><br></pre></td></tr></table></figure></p>
<hr>
<h2 id="20-2-外键作用"><a href="#20-2-外键作用" class="headerlink" title="20.2 外键作用"></a>20.2 外键作用</h2><p>首先，给出父表和子表的定义：</p>
<ul>
<li>父表：指外键所指向的表；</li>
<li>子表：指相对于父表，拥有外键的表；<br>外键默认的作用有两个，分别对子表和父表进行约束。<br>第1种：约束子表<br>在子表进行数据的写操作（增和改）的时候，如果对应的外键字段在父表中找不到对用的匹配，那么操作会失败。</li>
</ul>
<p>实例：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">ERROR 1064 (42000): You have an error in your SQL syntax; <span class="keyword">check</span> the <span class="keyword">manual</span> that corresponds <span class="keyword">to</span> your MySQL <span class="keyword">server</span> <span class="keyword">version</span> <span class="keyword">for</span> the <span class="keyword">right</span> syntax <span class="keyword">to</span> <span class="keyword">use</span> near <span class="string">'ls'</span> <span class="keyword">at</span> line <span class="number">1</span></span><br><span class="line">mysql&gt; <span class="keyword">insert</span> <span class="keyword">into</span> my_foreign1 <span class="keyword">values</span>(<span class="literal">null</span>, <span class="string">'Harlon'</span>, <span class="number">1</span>);</span><br><span class="line">ERROR 1452 (23000): Cannot add or <span class="keyword">update</span> a <span class="keyword">child</span> <span class="keyword">row</span>: a <span class="keyword">foreign</span> <span class="keyword">key</span> <span class="keyword">constraint</span> fails (<span class="string">`test`</span>.<span class="string">`my_foreign1`</span>, <span class="keyword">CONSTRAINT</span> <span class="string">`test_foreign`</span> <span class="keyword">FOREIGN</span> <span class="keyword">KEY</span> (<span class="string">`c_id`</span>) <span class="keyword">REFERENCES</span> <span class="string">`my_class`</span> (<span class="string">`id`</span>))</span><br><span class="line">mysql&gt; <span class="keyword">insert</span> <span class="keyword">into</span> my_class <span class="keyword">values</span>(<span class="literal">null</span>, <span class="string">'class-1'</span>, <span class="string">'A301'</span>);</span><br><span class="line">Query OK, 1 row affected (0.00 sec)</span><br><span class="line"></span><br><span class="line">mysql&gt; insert into my_foreign1 values(null, 'Harlon', 1);</span><br><span class="line">Query OK, 1 row affected (0.01 sec)</span><br></pre></td></tr></table></figure></p>
<p>第2中：约束父表<br>在父表进行数据的写操作（删和改，且涉及主键）的时候，如果对应的主键在子表中已经被数据引用，那么操作就会失败。  </p>
<p>实例：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">update</span> my_class <span class="keyword">set</span> <span class="keyword">id</span> = <span class="number">5</span> <span class="keyword">where</span> <span class="keyword">id</span> = <span class="number">1</span>;</span><br></pre></td></tr></table></figure></p>
<hr>
<h2 id="20-3-外键条件"><a href="#20-3-外键条件" class="headerlink" title="20.3 外键条件"></a>20.3 外键条件</h2><p>在我们使用外键的时候，应该遵循如下条件：</p>
<ul>
<li>外键要存在，首先必须保证表的引擎是InnoDB（默认的存储引擎），如果不是InnoDB存储引擎，那么主键可以创建成功，但没有约束作用；</li>
<li>外键的字段的字段类型（列类型），必须与父表的主键类型完全一致；</li>
<li>每张表中的外键名称不能重复；</li>
<li>增加外键的字段，如果数据已经存在，那么要要求数据与父表中的主键对应；</li>
</ul>
<hr>
<h2 id="20-4-外键约束"><a href="#20-4-外键约束" class="headerlink" title="20.4 外键约束"></a>20.4 外键约束</h2><p>所谓的外键约束，就是指外键的作用。之前所讲的外键的作用都是默认的作用，实际上，可以通过对外键的需求，进行定制操作。<br>外键约束有三种模式，分别为：</p>
<ul>
<li><code>distrinct</code>：严格模式（默认），父表不能删除或更新一个已经被子表数据所引用的记录</li>
<li><code>cascade</code>：级联模式，父表的操作，对应父表关联的数据也跟着删除</li>
<li><code>set null</code>：置空模式，父表的操作之后，子表对用的数据（外键字段）被置空</li>
</ul>
<p>基本语法：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">foreign key(外键字段) references 父表(主键字段) [on <span class="keyword">delete</span> 模式 <span class="keyword">on</span> <span class="keyword">update</span> 模式];</span><br></pre></td></tr></table></figure></p>
<p>通常一个合理的做法（约束模式）是：删除的时候，子表被置空；更新的时候，子表进行级联操作。</p>
<p>实例：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">create</span> <span class="keyword">table</span> my_foreign4 (</span><br><span class="line">    <span class="keyword">id</span> <span class="built_in">int</span> primary <span class="keyword">key</span> auto_increment,</span><br><span class="line">    <span class="keyword">name</span> <span class="built_in">varchar</span>(<span class="number">20</span>) <span class="keyword">not</span> <span class="literal">null</span>,</span><br><span class="line">    c_id <span class="built_in">int</span>,</span><br><span class="line">    <span class="keyword">foreign</span> <span class="keyword">key</span>(c_id) <span class="keyword">references</span> my_class(<span class="keyword">id</span>) <span class="keyword">on</span> <span class="keyword">delete</span> <span class="keyword">set</span> <span class="literal">null</span> <span class="keyword">on</span> <span class="keyword">update</span> <span class="keyword">cascade</span></span><br><span class="line">) <span class="keyword">engine</span> <span class="keyword">InnoDB</span> <span class="keyword">charset</span> utf8;</span><br></pre></td></tr></table></figure></p>
<p>外键的功能十分强大，但是在开发过程中，由于外键的存在，使得开发变得困难不可控，所以一般都不使用外键。</p>
<hr>
<h1 id="21、联合查询"><a href="#21、联合查询" class="headerlink" title="21、联合查询"></a>21、联合查询</h1><p>联合查询：<code>union</code>，将多次查询（多条select语句）的结果，在字段数相同的情况下，在记录的层次上进行拼接。<br>基本语法：<br>联合查询由多条<code>select</code>语句构成，每条<code>select</code>语句获取的字段数相同，但与字段类型无关。<br>基本语法：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> 语句<span class="number">1</span> <span class="keyword">union</span> [<span class="keyword">union</span>选项] <span class="keyword">select</span> 语句<span class="number">2</span>;</span><br></pre></td></tr></table></figure></p>
<p>实例：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">mysql&gt; select * from my_class union distinct select * from my_class;</span><br><span class="line">+<span class="comment">----+---------+------+</span></span><br><span class="line">| id | grade   | room |</span><br><span class="line">+<span class="comment">----+---------+------+</span></span><br><span class="line">|  1 | class-1 | A301 |</span><br><span class="line">+<span class="comment">----+---------+------+</span></span><br><span class="line">1 row in <span class="keyword">set</span> (<span class="number">0.00</span> sec)</span><br><span class="line"></span><br><span class="line">mysql&gt; <span class="keyword">select</span> * <span class="keyword">from</span> my_class <span class="keyword">union</span> <span class="keyword">all</span> <span class="keyword">select</span> * <span class="keyword">from</span> my_class;</span><br><span class="line">+<span class="comment">----+---------+------+</span></span><br><span class="line">| id | grade   | room |</span><br><span class="line">+<span class="comment">----+---------+------+</span></span><br><span class="line">|  1 | class-1 | A301 |</span><br><span class="line">|  1 | class-1 | A301 |</span><br><span class="line">+<span class="comment">----+---------+------+</span></span><br></pre></td></tr></table></figure></p>
<p>联合查询只要求字段数相同，而跟类型无关。执行如下<code>SQL</code>语句，实例：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> <span class="keyword">id</span>, grade, room <span class="keyword">from</span> my_class <span class="keyword">union</span> <span class="keyword">distinct</span> <span class="keyword">select</span> <span class="keyword">name</span>, grade, <span class="keyword">id</span> <span class="keyword">from</span> Student;</span><br></pre></td></tr></table></figure></p>
<p>意义：  </p>
<ul>
<li>查询同一张表，例如查询学生信息，要求男孩按年龄升序排序，女生按年龄降序排序</li>
<li>多表查询，多张表的结构是完全一样的，保持的数据结构也是一样的</li>
</ul>
<p>此外，如果数据量非常的大，就要进行分表（垂直分表和水平分表），而分表的依据无外乎数据多不多和常不常用。  </p>
<hr>
<h1 id="22、子查询"><a href="#22、子查询" class="headerlink" title="22、子查询"></a>22、子查询</h1><p>子查询：<code>sub query</code>，查询时在某个查询结果上进行的，一条select语句内部包含了另外一条select语句。  </p>
<p>分类：<br>子查询有两种分类方式，分别为：按结果分类和位置分类。<br>按结果分类，即根据子查询得到的数据进行分类（理论上，任何一个查询结果都可以理解为一个二维表），分别为：</p>
<ul>
<li>标量子查询：子查询得到的结果是一行一列，出现的位置在<code>where</code>之后</li>
<li>列子查询：子查询得到的结果是一行多列，出现的位置在<code>where</code>之后</li>
<li>行子查询：子查询得到的结果是多行一列（多行多列），出现的位置在<code>where</code>之后</li>
<li>表子查询：子查询得到的结果是多行多列，出现的位置在<code>from</code>之后。</li>
</ul>
<p>按位置分类，即根据子查询（<code>select</code>语句）在外部查询（<code>select</code>语句）中出现的位置进行分类，分别为：</p>
<ul>
<li><code>from</code>子查询：子查询出现在<code>from</code>之后</li>
<li><code>where</code>子查询：子查询出现在<code>where</code>条件之后</li>
<li><code>exists</code>子查询：子查询出现在<code>exists</code>里面</li>
</ul>
<h2 id="22-1-标量子查询"><a href="#22-1-标量子查询" class="headerlink" title="22.1 标量子查询"></a>22.1 标量子查询</h2><p>需求：知道班级号，想要获取该班的全部学生。<br>思路：  </p>
<ul>
<li><p>先确定数据源，学生表</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> * <span class="keyword">from</span> student <span class="keyword">where</span> c_id = ?;</span><br></pre></td></tr></table></figure>
</li>
<li><p>然后获取班级ID，通过班级表来确定</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> <span class="keyword">id</span> <span class="keyword">from</span> <span class="keyword">class</span> <span class="keyword">where</span> grade = <span class="string">"class-1"</span>;</span><br></pre></td></tr></table></figure>
</li>
<li><p>合并</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> * <span class="keyword">from</span> student <span class="keyword">where</span> c_id = (<span class="keyword">select</span> <span class="keyword">id</span> <span class="keyword">from</span> <span class="keyword">class</span> <span class="keyword">where</span> grade = <span class="string">'class-1'</span>);</span><br></pre></td></tr></table></figure>
</li>
</ul>
<h2 id="22-2-列子查询"><a href="#22-2-列子查询" class="headerlink" title="22.2 列子查询"></a>22.2 列子查询</h2><p>需求：查询所有在读班级（学生表存在的班级）的学生。<br>思路：  </p>
<ul>
<li><p>先确定数据源，学生表</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> * <span class="keyword">from</span> student <span class="keyword">where</span> c_id <span class="keyword">in</span> ?;</span><br></pre></td></tr></table></figure>
</li>
<li><p>然后确定全部有效的班级ID</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> <span class="keyword">id</span> <span class="keyword">from</span> <span class="keyword">class</span>;</span><br></pre></td></tr></table></figure>
</li>
<li><p>合并：</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> * <span class="keyword">from</span> student <span class="keyword">where</span> c_id <span class="keyword">in</span> (<span class="keyword">select</span> <span class="keyword">id</span> <span class="keyword">from</span> <span class="keyword">class</span>);</span><br></pre></td></tr></table></figure>
</li>
</ul>
<p>在列子查询的结果为一行多列时，我们需要使用<code>in</code>作为条件来进行匹配；此外，在MySQL中还有三个类似的条件，分别为：<code>all</code>、<code>some</code>和<code>any</code>。</p>
<ul>
<li><code>any</code>等价于<code>in</code>，表示其中一个；</li>
<li><code>any</code>等价于<code>some</code>，而<code>any</code>和<code>some</code>用于否定时却有些区别</li>
<li><code>all</code>表示等于全部</li>
</ul>
<p>值得注意的是，在我们使用上面三个关键字中任何一个的时候，都需要搭配<code>=</code>使用，例如：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> * <span class="keyword">from</span> student <span class="keyword">where</span> c_id = <span class="keyword">any</span> (<span class="keyword">select</span> <span class="keyword">id</span> <span class="keyword">from</span> <span class="keyword">class</span>);</span><br><span class="line"><span class="keyword">select</span> * <span class="keyword">from</span> student <span class="keyword">where</span> c_id = <span class="keyword">some</span> (<span class="keyword">select</span> <span class="keyword">id</span> <span class="keyword">from</span> <span class="keyword">class</span>);</span><br><span class="line"><span class="keyword">select</span> * <span class="keyword">from</span> student <span class="keyword">where</span> c_id = <span class="keyword">all</span> (<span class="keyword">select</span> <span class="keyword">id</span> <span class="keyword">from</span> <span class="keyword">class</span>);</span><br></pre></td></tr></table></figure></p>
<p>否定用法：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> * <span class="keyword">from</span> student <span class="keyword">where</span> c_id != <span class="keyword">any</span> (<span class="keyword">select</span> <span class="keyword">id</span> <span class="keyword">from</span> <span class="keyword">class</span>);</span><br><span class="line"><span class="keyword">select</span> * <span class="keyword">from</span> student <span class="keyword">where</span> c_id != <span class="keyword">some</span> (<span class="keyword">select</span> <span class="keyword">id</span> <span class="keyword">from</span> <span class="keyword">class</span>);</span><br><span class="line"><span class="keyword">select</span> * <span class="keyword">from</span> student <span class="keyword">where</span> c_id != <span class="keyword">all</span> (<span class="keyword">select</span> <span class="keyword">id</span> <span class="keyword">from</span> <span class="keyword">class</span>);</span><br></pre></td></tr></table></figure></p>
<hr>
<h2 id="22-3-行子查询"><a href="#22-3-行子查询" class="headerlink" title="22.3 行子查询"></a>22.3 行子查询</h2><p>行子查询，返回的结果可以是一行多列或者多列多行。<br>需求：查询学生表中，年龄最大且身高最高的学生。<br>思路：  </p>
<ul>
<li><p>先确定数据源，学生表</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> * <span class="keyword">from</span> student <span class="keyword">where</span> age = ? <span class="keyword">and</span> height = ?;</span><br></pre></td></tr></table></figure>
</li>
<li><p>然后确定最大年龄和最大身高</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> <span class="keyword">max</span>(age), <span class="keyword">max</span>(height) <span class="keyword">from</span> student;</span><br></pre></td></tr></table></figure>
</li>
<li><p>合并</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> * <span class="keyword">from</span> student <span class="keyword">where</span> (age, height) = (<span class="keyword">select</span> <span class="keyword">max</span>(age), <span class="keyword">max</span>(height) <span class="keyword">from</span> student);</span><br></pre></td></tr></table></figure>
</li>
</ul>
<hr>
<h2 id="22-4-表子查询"><a href="#22-4-表子查询" class="headerlink" title="22.4 表子查询"></a>22.4 表子查询</h2><p>表子查询，返回的结果是多行多列二维表（将子查询的结果当做二维表来使用），实际上查询的返回结果都可以称之为二维表。<br>需求：找出每个班身高最高的学生。<br>思路：  </p>
<ul>
<li><p>先确定数据源，将学生按身高进行降序排序</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> * <span class="keyword">from</span> student <span class="keyword">order</span> <span class="keyword">by</span> height <span class="keyword">desc</span>;</span><br></pre></td></tr></table></figure>
</li>
<li><p>从每个班级选出第一个学生</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> * <span class="keyword">from</span> student <span class="keyword">group</span> <span class="keyword">by</span> c_id;</span><br></pre></td></tr></table></figure>
</li>
<li><p>合并</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> * <span class="keyword">from</span> (<span class="keyword">select</span> * <span class="keyword">from</span> student <span class="keyword">order</span> <span class="keyword">by</span> height <span class="keyword">desc</span>) <span class="keyword">as</span> student <span class="keyword">group</span> <span class="keyword">by</span> c_id;</span><br></pre></td></tr></table></figure>
</li>
</ul>
<hr>
<h2 id="22-5-exists子查询"><a href="#22-5-exists子查询" class="headerlink" title="22.5 exists子查询"></a>22.5 exists子查询</h2><p><code>exists</code>：表示是否存在的意思，因此<code>exists</code>子查询就是用来判断某些条件是否满足（跨表），<code>exists</code>是接在<code>where</code>之后，其返回的结果为<code>1</code>或<code>0</code>，满足条件为<code>1</code>，反之为<code>0</code>。<br>需求：在班级存在的前提下，查询所有的学生。<br>思路：  </p>
<ul>
<li><p>先确定数据源：</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> * <span class="keyword">from</span> student <span class="keyword">where</span> ?;</span><br></pre></td></tr></table></figure>
</li>
<li><p>然后确定条件是否满足</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">exists(<span class="keyword">select</span> * <span class="keyword">from</span> <span class="keyword">class</span>);</span><br></pre></td></tr></table></figure>
</li>
<li><p>合并：</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> * <span class="keyword">from</span> student <span class="keyword">where</span> <span class="keyword">exists</span>(<span class="keyword">select</span> * <span class="keyword">from</span> <span class="keyword">class</span>);</span><br><span class="line"></span><br><span class="line"><span class="keyword">select</span> * <span class="keyword">from</span> student <span class="keyword">where</span> <span class="keyword">exists</span>(<span class="keyword">select</span> * <span class="keyword">from</span> <span class="keyword">class</span> <span class="keyword">where</span> <span class="keyword">id</span>  = <span class="number">3</span>);</span><br><span class="line"></span><br><span class="line"><span class="keyword">select</span> * <span class="keyword">from</span> student <span class="keyword">where</span> <span class="keyword">exists</span>(<span class="keyword">select</span> * <span class="keyword">from</span> <span class="keyword">class</span> <span class="keyword">where</span> <span class="keyword">id</span> = <span class="number">100</span>);</span><br></pre></td></tr></table></figure>
</li>
</ul>
<hr>
<h1 id="23、视图"><a href="#23、视图" class="headerlink" title="23、视图"></a>23、视图</h1><p>视图：view，是一种有结构（有行有列），但没有结果（结构中不真实存在数据）的虚拟表，虚拟表的结构来源不是自己定义的，而是从对应的基表（视图的数据来源）中产生的。</p>
<h2 id="23-1-创建视图"><a href="#23-1-创建视图" class="headerlink" title="23.1 创建视图"></a>23.1 创建视图</h2><p>首先，给出创建视图的基本语法：<br>基本语法：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">create</span> <span class="keyword">view</span> 视图名 <span class="keyword">as</span> <span class="keyword">select</span> 语句;</span><br></pre></td></tr></table></figure></p>
<p>其中，select语句可以使普通查询，也可以是连接查询、联合查询、子查询等。<br>此外，视图根据数据的来源，可以分为单表视图和多表视图：  </p>
<ul>
<li>单表视图：基表只有一个</li>
<li>多表视图：基表至少两个</li>
</ul>
<p>实例：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">-- 单表视图</span></span><br><span class="line"><span class="keyword">create</span> <span class="keyword">view</span> my_v1 <span class="keyword">as</span> <span class="keyword">select</span> * <span class="keyword">from</span> student;</span><br><span class="line"><span class="keyword">create</span> <span class="keyword">view</span> my_v2 <span class="keyword">as</span> <span class="keyword">select</span> * <span class="keyword">from</span> my_class;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 多表视图</span></span><br><span class="line"><span class="keyword">create</span> <span class="keyword">view</span> my_v3 <span class="keyword">as</span> <span class="keyword">select</span> s.*, c.grade, c.room <span class="keyword">from</span> student <span class="keyword">as</span> s <span class="keyword">left</span> <span class="keyword">join</span> my_class c <span class="keyword">on</span> s._cid = c.id;</span><br></pre></td></tr></table></figure></p>
<hr>
<h2 id="23-2-查询视图"><a href="#23-2-查询视图" class="headerlink" title="23.2 查询视图"></a>23.2 查询视图</h2><p>这里的查询视图是指查看视图的结构，而不是查看视图的结果。<br>由于视图是一张虚拟表，因此标的所有查询语句，都使用于视图，例如：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">desc 视图名;</span><br><span class="line"><span class="keyword">show</span> <span class="keyword">create</span> <span class="keyword">table</span> 视图名;</span><br><span class="line"><span class="keyword">show</span> <span class="keyword">create</span> <span class="keyword">view</span> 视图名;</span><br></pre></td></tr></table></figure></p>
<p>此外，视图一旦创建，系统就会在视图对用的数据库文件夹下创建一个对应的frm结构文件，以保证结构的完整性。</p>
<hr>
<h2 id="23-3-使用视图"><a href="#23-3-使用视图" class="headerlink" title="23.3 使用视图"></a>23.3 使用视图</h2><p>在操作数据库表的过程中，使用视图，主要就是为了查询，因此将视图当做表一样查询即可。<br>在这里需要注意的是，虽然我们说视图是一个虚拟表，它不保存数据，但是它却可以获取数据。  </p>
<p>实例：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> * <span class="keyword">from</span> my_v1;</span><br><span class="line"><span class="keyword">select</span> * <span class="keyword">from</span> my_v2;</span><br><span class="line"><span class="keyword">select</span> * <span class="keyword">from</span> my_v3;</span><br></pre></td></tr></table></figure></p>
<p>我们查询视图的结果和查询创建视图时as后面连接的select语句的结果完全相同。<br>因此，我们也可以认为：创建视图，就是给一条select语句起别名，或者说是封装select语句。</p>
<hr>
<h2 id="23-4-修改视图"><a href="#23-4-修改视图" class="headerlink" title="23.4 修改视图"></a>23.4 修改视图</h2><p>视图本身不可修改，但是视图的来源（select）语句是可以修改的。因此，修改视图，就是修改视图的来源（select）语句。<br>基本语法：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">alter</span> <span class="keyword">view</span> 视图名 <span class="keyword">as</span> 新的<span class="keyword">select</span>语句;</span><br></pre></td></tr></table></figure></p>
<p>实例：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">alter</span> <span class="keyword">view</span> my_v1 <span class="keyword">as</span> <span class="keyword">select</span> <span class="keyword">id</span>, <span class="keyword">name</span>, gender, age, c_id <span class="keyword">from</span> student;</span><br></pre></td></tr></table></figure></p>
<hr>
<h2 id="23-5-删除视图"><a href="#23-5-删除视图" class="headerlink" title="23.5 删除视图"></a>23.5 删除视图</h2><p>基本语法：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">drop</span> <span class="keyword">view</span> 视图名;</span><br></pre></td></tr></table></figure></p>
<hr>
<h2 id="23-6-视图意义"><a href="#23-6-视图意义" class="headerlink" title="23.6 视图意义"></a>23.6 视图意义</h2><ul>
<li>视图可以节省SQL语句，将一条负责的查询语句来进行分装，以后可以直接对视图进行操作</li>
<li>数据安全，视图操作主要是针对查询的，如果对视图结构进行处理，例如删除，并不会影响基表的数据</li>
<li>视图往往在大项目中使用，而且是多系统使用，可以对外提供有用的数据，但是隐藏关键（或无用）的数据</li>
<li>视图是对外提供友好型的，不同的视图提供不同额数据，就如专门对外设计的一样</li>
<li>视图可以更好（或者说，容易）进行权限控制</li>
</ul>
<hr>
<h1 id="24、视图数据操作"><a href="#24、视图数据操作" class="headerlink" title="24、视图数据操作"></a>24、视图数据操作</h1><p>视图数据操作：虽然我们说视图可以称之为select语句的别名，实际上，它和别名不一样，因为视图是可以进行数据写操作的，只不多有很多限制而已。</p>
<hr>
<h2 id="24-1-新增数据"><a href="#24-1-新增数据" class="headerlink" title="24.1 新增数据"></a>24.1 新增数据</h2><p>在这里，新增数据就是指通过视图直接对基表进行数据的新增操作。  </p>
<ul>
<li>限制1：多表视图不能进行新增操作</li>
<li>限制2：可以向单表视图新增数据，但视图中包含的字段必须有基表中所有不能为空的字段</li>
</ul>
<hr>
<h2 id="24-2-删除数据"><a href="#24-2-删除数据" class="headerlink" title="24.2 删除数据"></a>24.2 删除数据</h2><p>与新增数据类似：</p>
<ul>
<li>多表不能删除数据</li>
<li>单表数据可以删除数据</li>
</ul>
<hr>
<h2 id="24-3-更新数据"><a href="#24-3-更新数据" class="headerlink" title="24.3 更新数据"></a>24.3 更新数据</h2><p>理论上，无论多表视图还是单表视图，都可以进行数据的更新。<br>此外，更新数据并不总是成功的，这是因为有更新限制的存在。<br>更新限制：with check option，如果创建视图的时候，设置了某个字段的限制，那么对视图进行更新操作的时候，系统就会进行验证，要保证更新之后，数据依然可以被查出来，否则不让更新。</p>
<p>实例：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">create</span> <span class="keyword">view</span> my_v4 <span class="keyword">as</span> <span class="keyword">select</span> * <span class="keyword">from</span> student <span class="keyword">where</span> height &gt; <span class="number">170</span> <span class="keyword">with</span> <span class="keyword">check</span> <span class="keyword">option</span>;</span><br><span class="line"><span class="keyword">update</span> my_v4 <span class="keyword">set</span> height = <span class="number">165</span> <span class="keyword">where</span> <span class="keyword">id</span> = <span class="number">6</span>;</span><br></pre></td></tr></table></figure></p>
<hr>
<h2 id="24-4-视图算法"><a href="#24-4-视图算法" class="headerlink" title="24.4 视图算法"></a>24.4 视图算法</h2><p>视图算发，即系统对视图以及外部查询视图的select语句的一种解析方式，视图算法有三种，分别为：  </p>
<ul>
<li>underfined：未定义（默认的），这不是一种实际使用的算法，而是一个“推卸责任”的算法。在未定义的情况下，告诉系统，视图没有定义算法，请自己选择</li>
<li>template：临时表算法，系统先执行视图的select语句，后执行外部查询语句</li>
<li>merger：合并算法，系统先将视图对用的select语句与外部查询视图的select语句进行合并，然后再执行。此算法比较高效，且在未定义算法的时候，经常会默认选择此算法。<br>指定视图算法，基本语法：<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">create</span> [algorithm = <span class="keyword">template</span>/<span class="keyword">merge</span>/underfined] <span class="keyword">view</span> 视图名 <span class="keyword">as</span> <span class="keyword">select</span> 语句;</span><br></pre></td></tr></table></figure>
</li>
</ul>
<hr>
<h1 id="25、数据备份与还原"><a href="#25、数据备份与还原" class="headerlink" title="25、数据备份与还原"></a>25、数据备份与还原</h1><p>基础概念：</p>
<ul>
<li>备份：将当前已有的数据或记录存一份</li>
<li>还原：将数据恢复到备份时的状态<br>为什么要进行数据备份与还原：  </li>
<li>防止数据丢失</li>
<li>保护数据记录<br>数据备份与还原的方式有很多，具体可以分为：数据表备份、单表数据备份，SQL备份和增量备份。<h2 id="25-1-数据表备份"><a href="#25-1-数据表备份" class="headerlink" title="25.1 数据表备份"></a>25.1 数据表备份</h2>数据表备份，不需要通过 SQL 来备份，我们可以直接进入到数据库文件夹复制对应的表结构以及数据；在需要还原数据的时候，直接将备份（复制）的内容放回去即可。<br>不过想要进行数据表备份是有前提条件的，因为不同的存储引擎之间是有区别的。<br>对于存储引擎，MySQL 主要使用两种，分别为：InnoDB 和 Myisam，两者均免费。  </li>
</ul>
<table>
<thead>
<tr>
<th>特点</th>
<th>MyISAM</th>
<th>InnoDB</th>
<th>BDB</th>
<th>Memory</th>
<th>Archive</th>
</tr>
</thead>
<tbody>
<tr>
<td>批量插入的速度</td>
<td>搞</td>
<td>低</td>
<td>高</td>
<td>高</td>
<td>非常高</td>
</tr>
<tr>
<td>事务安全</td>
<td>——</td>
<td>支持</td>
<td>支持</td>
<td>——</td>
<td>——</td>
</tr>
<tr>
<td>全文索引</td>
<td>支持</td>
<td>5.5版本支持</td>
<td>——</td>
<td>——</td>
<td>——</td>
</tr>
<tr>
<td>锁机制</td>
<td>表锁</td>
<td>行锁</td>
<td>页锁</td>
<td>表锁</td>
<td>行锁</td>
</tr>
<tr>
<td>存储限制</td>
<td>没有</td>
<td>64TB</td>
<td>没有</td>
<td>有</td>
<td>没有</td>
</tr>
<tr>
<td>B树索引</td>
<td>支持</td>
<td>支持</td>
<td>支持</td>
<td>支持</td>
<td>——</td>
</tr>
<tr>
<td>哈希索引</td>
<td>——</td>
<td>支持</td>
<td>——</td>
<td>支持</td>
<td>——</td>
</tr>
<tr>
<td>集群索引</td>
<td>——</td>
<td>支持</td>
<td>——</td>
<td>——</td>
<td>——</td>
</tr>
<tr>
<td>数据索引</td>
<td>——</td>
<td>支持</td>
<td>——</td>
<td>支持</td>
<td>——</td>
</tr>
<tr>
<td>索引缓存</td>
<td>支持</td>
<td>支持</td>
<td>——</td>
<td>支持</td>
<td>——</td>
</tr>
<tr>
<td>数据可压缩</td>
<td>支持</td>
<td>——</td>
<td>——</td>
<td>——</td>
<td>支持</td>
</tr>
<tr>
<td>空间使用</td>
<td>低</td>
<td>高</td>
<td>低</td>
<td>N/A</td>
<td>非常低</td>
</tr>
<tr>
<td>内存使用</td>
<td>低</td>
<td>高</td>
<td>低</td>
<td>中等</td>
<td>低</td>
</tr>
<tr>
<td>外键支持</td>
<td>——</td>
<td>支持</td>
<td>——</td>
<td>——</td>
<td>——</td>
</tr>
</tbody>
</table>
<p>其中，MyISAM和InnoDB的数据存储方式也有所区别：</p>
<ul>
<li>MyISAM：表、索引和数据全部分开存储</li>
<li>InnoDB：只有表结构，数据全部存储在ibd文件中</li>
</ul>
<p>在linux上，MySQL文件默认存储在/var/lib/mysql中，例如：<br><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">[root@bigdata4 test]# ls</span><br><span class="line">db.opt        my_class.ibd  my_date.ibd     my_default.ibd  my_float.ibd     my_foreign2.ibd  my_foreign4.frm  my_int.frm   my_pri3.frm  my_set.frm      my_unique3.frm  my_v1.frm</span><br><span class="line">my_auto.frm   my_copy.frm   my_decimal.frm  my_enum.frm     my_foreign1.frm  my_foreign3.frm  my_foreign4.ibd  my_int.ibd   my_pri3.ibd  my_set.ibd      my_unique3.ibd  my_v2.frm</span><br><span class="line">my_auto.ibd   my_copy.ibd   my_decimal.ibd  my_enum.ibd     my_foreign1.ibd  my_foreign3.MYD  my_friend.frm    my_pri2.frm  my_pri.frm   my_unique2.frm  my_unique.frm   Student.frm</span><br><span class="line">my_class.frm  my_date.frm   my_default.frm  my_float.frm    my_foreign2.frm  my_foreign3.MYI  my_friend.ibd    my_pri2.ibd  my_pri.ibd   my_unique2.ibd  my_unique.ibd   Student.ibd</span><br></pre></td></tr></table></figure></p>
<p>其中：</p>
<ul>
<li>*.frm：存储表的结构</li>
<li>*.MYD：存储表的数据</li>
<li>*.MYI：存储表的索引</li>
</ul>
<p>在这里，有一点需要我们注意，那就是：我们可以将通过 InnoDB 存储引擎产生的.frm和.idb文件复制到另一个数据库，也可以通过show tables命令查看复制过来的表名称，但是却无法获得数据。  </p>
<hr>
<h2 id="25-2-单数据表备份"><a href="#25-2-单数据表备份" class="headerlink" title="25.2 单数据表备份"></a>25.2 单数据表备份</h2><p>单数据表备份，每次只能备份一张表，而且只能备份数据，不能备份表结构。<br>通常的使用场景：将表中的数据导出到文件。<br>备份方法：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> */字段列表 <span class="keyword">into</span> <span class="keyword">outfile</span> <span class="string">'文件存储路径'</span> <span class="keyword">from</span> 数据源;</span><br><span class="line"><span class="keyword">select</span> */字段列表 <span class="keyword">from</span> 数据源 <span class="keyword">into</span> <span class="keyword">outfile</span> <span class="string">'文件存储路径'</span>;</span><br></pre></td></tr></table></figure></p>
<p>在这里，使用单表数据备份有一个前提，那就是：导出的外部文件不存在，即文件存储路径下的文件不存在。  </p>
<p>实例：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> * <span class="keyword">into</span> <span class="keyword">outfile</span> <span class="string">'/home/class.txt'</span> <span class="keyword">from</span> my_class;</span><br></pre></td></tr></table></figure></p>
<p>单表数据备份的高级操作，即自己指定字段和行的处理方式。<br>基本语法：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> */字段列表 <span class="keyword">into</span> <span class="keyword">outfile</span>  <span class="string">'文件存储路径'</span> <span class="keyword">fields</span> 字段处理 <span class="keyword">lines</span> 行处理 <span class="keyword">from</span> 数据源;</span><br></pre></td></tr></table></figure></p>
<p>字段处理：</p>
<ul>
<li>enclosed by：指定字段用什么内容包括，模式是空字符串</li>
<li>terminated by：指定字段以什么技术，默认是\t</li>
<li>escaped by：指定特殊符号用什么方式处理，默认是\</li>
</ul>
<p>行处理：</p>
<ul>
<li>starting by：指定每行以什么开始，默认是空字符串</li>
<li>terminated by：指定每行以什么结束，默认是\r\n</li>
</ul>
<p>实例：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> * <span class="keyword">into</span> <span class="keyword">outfile</span> <span class="string">'/home/class.txt'</span> <span class="keyword">fields</span> <span class="keyword">enclosed</span> <span class="keyword">by</span> <span class="string">'"'</span> <span class="keyword">terminated</span> <span class="keyword">by</span> <span class="string">' | '</span> <span class="keyword">lines</span> <span class="keyword">starting</span> <span class="keyword">by</span> <span class="string">'START: '</span> <span class="keyword">from</span> my_class;</span><br></pre></td></tr></table></figure></p>
<p>恢复数据，基本语法：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">load</span> <span class="keyword">data</span> <span class="keyword">infile</span> <span class="string">'文件路径'</span> <span class="keyword">into</span> <span class="keyword">table</span> 表名 字段列表 <span class="keyword">fields</span> 字段处理 <span class="keyword">lines</span> 行处理;</span><br></pre></td></tr></table></figure></p>
<hr>
<h2 id="25-3-SQL备份"><a href="#25-3-SQL备份" class="headerlink" title="25.3 SQL备份"></a>25.3 SQL备份</h2><p>SQL备份，备份的是SQL语句，进行SQL备份的时候，系统会对表结构以及数据进行处理，变成相应的SQL语句，然后执行备份，在还原的时候，只要执行备份的SQL语句即可，此种备份方式主要是针对表结构。<br>不过，MySQL并没有提供SQL备份的指令，如果我们想要进行SQL备份，则需要利用MySQL提供的软件mysqldump。<br>基本语法：<br><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">mysqldump -hPup 数据库名字 [表名1, [表名2] &gt; 备份文件目录</span><br></pre></td></tr></table></figure></p>
<p>其中，-hPup分别为：</p>
<ul>
<li>h：IP或者localhost</li>
<li>P：端口号</li>
<li>u：用户名</li>
<li>p：密码</li>
</ul>
<p>实例：<br><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">mysqldump -uroot -p123456 test class &gt; class.sql</span><br></pre></td></tr></table></figure></p>
<p>还原数据，基本语法：<br>方法1：使用mysql还原数据<br><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">mysql -hPup 数据库名称 [表名1 [表名2]] &lt; 备份目录</span><br></pre></td></tr></table></figure></p>
<p>实例：<br><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">mysql -uroot -p123456 test &lt; class.sql</span><br></pre></td></tr></table></figure></p>
<p>方法2：使用SQL命令还原数据<br>基本语法：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">source 备份文件目录;</span><br></pre></td></tr></table></figure></p>
<p>实例：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">source /home/class.sql</span><br></pre></td></tr></table></figure></p>
<p>SQL备份的优缺点：</p>
<ul>
<li>优点：可以备份表结构</li>
<li>缺点：增加额外的SQL命令，会浪费磁盘空间</li>
</ul>
<hr>
<h2 id="25-4-增量备份"><a href="#25-4-增量备份" class="headerlink" title="25.4 增量备份"></a>25.4 增量备份</h2><p>增量备份，不是针对数据或者 SQL 进行备份，而是针对 MySQL 服务器的日志进行备份，其日志内容包括了我们对数据库的各种操作的历史记录，如增删改查等。此外，增量备份是指定时间段进行备份，因此备份的数据一般不会出现重复的情况，常用于大型项目的数据备份。</p>
<hr>
<h1 id="26、事务"><a href="#26、事务" class="headerlink" title="26、事务"></a>26、事务</h1><p>事务：一系列将要发生或正在发生的连续操作。<br>事务安全：是一种保护连续操作同时实现（完成）的机制，事务安全的意义就是，保证数据操作的完整性。<br>创建银行账户并插入数据：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">create</span> <span class="keyword">table</span> bank_account(</span><br><span class="line">    <span class="keyword">id</span> <span class="built_in">int</span> primary <span class="keyword">key</span> auto_increment,</span><br><span class="line">    cardno <span class="built_in">varchar</span>(<span class="number">16</span>) <span class="keyword">not</span> <span class="literal">null</span> <span class="keyword">unique</span> <span class="keyword">comment</span> <span class="string">'bank card number'</span>,</span><br><span class="line">    <span class="keyword">name</span> <span class="built_in">varchar</span>(<span class="number">20</span>) <span class="keyword">not</span> <span class="literal">null</span>,</span><br><span class="line">    money <span class="built_in">decimal</span>(<span class="number">10</span>, <span class="number">2</span>) <span class="keyword">default</span>  <span class="number">0.0</span> <span class="keyword">comment</span> <span class="string">'account balance'</span></span><br><span class="line">) <span class="keyword">engine</span> <span class="keyword">InnoDB</span> <span class="keyword">charset</span> utf8;</span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> bank_account <span class="keyword">values</span></span><br><span class="line">(<span class="literal">null</span>, <span class="string">'0000000000000001'</span>, <span class="string">'Harlon'</span>, <span class="number">8000</span>),</span><br><span class="line">(<span class="literal">null</span>, <span class="string">'0000000000000002'</span>, <span class="string">'Jack'</span>, <span class="number">5000</span>);</span><br></pre></td></tr></table></figure></p>
<h2 id="26-1-事务操作"><a href="#26-1-事务操作" class="headerlink" title="26.1 事务操作"></a>26.1 事务操作</h2><p>事务操作，分为两种：自动事务，手动事务。<br>以银行的余额增减为例：<br>第1步，开启事务，告诉系统一下所有操作，不要直接写入数据，先保存到事务日志。<br>基本语法：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">start</span> <span class="keyword">transaction</span>;</span><br></pre></td></tr></table></figure></p>
<p>第2不，减少Harlon账户的余额<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">update</span> bank_account <span class="keyword">set</span> money = money - <span class="number">1000</span> <span class="keyword">where</span> <span class="keyword">id</span> = <span class="number">1</span>;</span><br><span class="line"><span class="keyword">select</span> * <span class="keyword">from</span> bank_account;</span><br></pre></td></tr></table></figure></p>
<p>由于我们开启了事务操作，数据库中真实的数据，并没有同步更新，而是先写入事务日志。<br>第3步：增加Jack账户的余额<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">update</span> bank_account <span class="keyword">set</span> money = money + <span class="number">1000</span> <span class="keyword">where</span> <span class="keyword">id</span> = <span class="number">2</span>;</span><br><span class="line"><span class="keyword">select</span> * <span class="keyword">from</span> bank_account;</span><br></pre></td></tr></table></figure></p>
<p>第4步：提交事务或回滚事务</p>
<ul>
<li>提交事务：commit</li>
<li>回滚事务：rollback</li>
</ul>
<p>如果我们选择提交事务，则将事务日志存储的记录直接更新到数据库，并清除事务日志；如果我们选择回滚事务，则直接将事务日志清除，所有在开启事务至回滚事务之间的操作失效，保持原有的数据库记录不变。在这里，我们以提交事务为例：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">commit</span>;</span><br><span class="line"><span class="keyword">select</span> * <span class="keyword">from</span> bank_account;</span><br></pre></td></tr></table></figure></p>
<p>目前，只有InnoDB支持事务操作。</p>
<hr>
<h2 id="26-2-事务原理"><a href="#26-2-事务原理" class="headerlink" title="26.2 事务原理"></a>26.2 事务原理</h2><p>事务原理：在事务开启之后，所有的操作都会被临时存储到事务日志，事务日志只有在收到commit命令之后，才会将操作同步到数据表，其他任何情况下都会清空事务日志，例如突然断开连接、收到rollback命令等。<br>接下来，简单分析一下MySQL的操作过程：  </p>
<ul>
<li>Step1：客户端与服务端建立连接，同时开启一个临时的事务日志，此事务日志只作用于当前用户的当次连接；</li>
<li>Step2：在客户端用 SQL 语句执行写操作，客户端收到 SQL 语句，执行，将结果直接写入到数据表，并将数据表同步到数据库；</li>
<li>Step3：我们在客户端开启事务，则服务端原来的操作机制被改变，后续所有操作都会被先写入到临时日志文件；</li>
<li>Step4：在客户端执行 SQL 语句（例如写操作），服务端收到 SQL 语句，执行，将结果写入到临时日志文件，并不将结果同步到数据库；</li>
<li>Step5：在客户端执行查询操作，服务端直接从临时日志文件中捞取数据，返回给客户端；</li>
<li>Step6：在客户端执行commit或者rollback命令，清空临时日志文件，如果是commit命令，则将结果同步到数据库；如果是rollback命令，则不同步。</li>
</ul>
<hr>
<h2 id="26-3-回滚点"><a href="#26-3-回滚点" class="headerlink" title="26.3 回滚点"></a>26.3 回滚点</h2><p>回滚点：在某个操作成功完成之后，后续的操作有可能成功也有可能失败，但无论后续操作的结果如何，前一次操作都已经成功，因此我们可以在当前成功的位置，设置一个操作点，其可以供后续操作返回该位置，而不是返回所有操作，这个点称之为回滚点。关于回滚点的基本语法为：</p>
<ul>
<li>设置回滚点：savepoint 回滚点名称</li>
<li>返回回滚点：rollback to 回滚点名称</li>
</ul>
<p>实例：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> * <span class="keyword">from</span> bank_account;</span><br><span class="line"><span class="keyword">start</span> <span class="keyword">transaction</span>;</span><br><span class="line"><span class="comment">-- 发工资</span></span><br><span class="line"><span class="keyword">update</span> bank_account <span class="keyword">set</span> money = money + <span class="number">10000</span> <span class="keyword">where</span> <span class="keyword">id</span> = <span class="number">1</span>;</span><br><span class="line"><span class="keyword">savepoint</span> spone;</span><br><span class="line"><span class="comment">-- 扣税，错误</span></span><br><span class="line"><span class="keyword">update</span> bank_account <span class="keyword">set</span> money = money - <span class="number">10000</span> * <span class="number">0.05</span> <span class="keyword">where</span> <span class="keyword">id</span> = <span class="number">2</span>;</span><br><span class="line"><span class="keyword">select</span> * <span class="keyword">from</span> bank_account;</span><br><span class="line"><span class="keyword">rollback</span> <span class="keyword">to</span> spone;</span><br><span class="line"><span class="comment">-- 扣税，成功</span></span><br><span class="line"><span class="keyword">update</span> bank_account <span class="keyword">set</span> money = money - <span class="number">10000</span> * <span class="number">0.05</span> <span class="keyword">where</span> <span class="keyword">id</span> = <span class="number">1</span>;</span><br><span class="line"><span class="keyword">select</span> * <span class="keyword">from</span> bank_account;</span><br><span class="line"><span class="keyword">commit</span>;</span><br></pre></td></tr></table></figure></p>
<hr>
<h2 id="26-4-自动事务"><a href="#26-4-自动事务" class="headerlink" title="26.4 自动事务"></a>26.4 自动事务</h2><p>在 MySQL 中，默认的都是自动事务处理，即用户在操作完成之后，其操作结果会立即被同步到数据库中。<br>自动事务是通过autocommit变量控制的，我们可以通过如下SQL语句，进行查看：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">show</span> <span class="keyword">variables</span> <span class="keyword">like</span> <span class="string">'autocommit'</span>;</span><br></pre></td></tr></table></figure></p>
<ul>
<li>开启自动事务处理：set autocommit = on / 1;</li>
<li>关闭自动事务处理：set autocommit = off / 0;</li>
</ul>
<hr>
<h2 id="26-5-事务特性"><a href="#26-5-事务特性" class="headerlink" title="26.5 事务特性"></a>26.5 事务特性</h2><p>事务的特性，可以概括为ACID，具体为：  </p>
<ul>
<li>原子性：Atomic，表示事务的整个操作都是一个整体，不可分割，要么全部成功，要么全部失败</li>
<li>一致性：Consistency，表示事务操作的前后，数据表中的数据处于一致状态</li>
<li>隔离性：Isolation，表示不同的事务操作之间是互相隔离的，互不影响</li>
<li>持久性：Durability，表示事务一旦提交，将不可修改，永久性的改变数据表中的数据</li>
</ul>
<hr>
<h1 id="27、数据库变量"><a href="#27、数据库变量" class="headerlink" title="27、数据库变量"></a>27、数据库变量</h1><p>在MySQL数据库中，变量有两种，分别为：系统变量和自定义变量。<br>根据变量的作用范围，又分为：  </p>
<ul>
<li>会话级别变量：仅对当前客户端当次连接有效</li>
<li>全局级别变量：对所有客户端的任一次连接有效<h2 id="27-1-系统变量"><a href="#27-1-系统变量" class="headerlink" title="27.1 系统变量"></a>27.1 系统变量</h2>系统变量，顾名思义，是系统设置好的变量（皆为全局级别变量），也是用来控制服务器表现的，如autocommit、wait_timeout等。<br>大多数的时候，我们并不需要使用系统变量，但我们仍然需要了解有这么回事，在必须要的时候，它可以帮助我们完成特殊的需求。<br>查看系统变量，语法为：  <figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">show</span> <span class="keyword">variables</span>;</span><br></pre></td></tr></table></figure>
</li>
</ul>
<p>查看具体的系统变量的值，语法为：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> @@变量名 [, @@变量名<span class="number">2</span>];</span><br></pre></td></tr></table></figure></p>
<p>实例：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">mysql&gt; select @@autocommit, @@version, @@version_compile_os, @@wait_timeout;</span><br><span class="line">+<span class="comment">--------------+-----------+----------------------+----------------+</span></span><br><span class="line">| @@autocommit | @@version | @@version_compile_os | @@wait_timeout |</span><br><span class="line">+<span class="comment">--------------+-----------+----------------------+----------------+</span></span><br><span class="line">|            1 | 5.6.39    | Linux                |          28800 |</span><br><span class="line">+<span class="comment">--------------+-----------+----------------------+----------------+</span></span><br></pre></td></tr></table></figure></p>
<p>修改会话级别变量，有两种方法，语法分别为：</p>
<ul>
<li>基本语法1：set 变量名 = 值</li>
<li>基本语法2：set @@变量名= 值</li>
</ul>
<p>对于修改全局级别变量，语法为：</p>
<ul>
<li>基本语法：set global 变量名= 值</li>
</ul>
<hr>
<h2 id="27-2-自定义变量"><a href="#27-2-自定义变量" class="headerlink" title="27.2 自定义变量"></a>27.2 自定义变量</h2><p>自定义变量，顾名思义，是用户自定义的变量，并且都是会话级别的变量。<br>系统为了区别系统变量与自定义变量，规定用户自定义的变量必须使用一个@符号，设置子定义变量的语法为：</p>
<ul>
<li>基本语法： set @变量名 = 值;</li>
</ul>
<p>实例：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">mysql&gt; set @name = 'harlon';</span><br><span class="line">Query OK, 0 rows affected (0.00 sec)</span><br><span class="line"></span><br><span class="line">mysql&gt; select @name;</span><br><span class="line">+<span class="comment">--------+</span></span><br><span class="line">| @name  |</span><br><span class="line">+<span class="comment">--------+</span></span><br><span class="line">| harlon |</span><br><span class="line">+<span class="comment">--------+</span></span><br><span class="line">1 row in <span class="keyword">set</span> (<span class="number">0.00</span> sec)</span><br></pre></td></tr></table></figure></p>
<p>注意：在 MySQL 中，很多地方会默认将=处理为比较符号，因此 MySQL 还提供了另外一种赋值符号:=，即冒号与等号拼接而成的符号。<br>此外，MySQL允许我们从数据表中获取数据，然后直接赋值给变量，共两种方式：<br>第1种：边复制，边查看结果。语法为：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> @变量名 := 字段名 <span class="keyword">from</span> 表名;</span><br></pre></td></tr></table></figure></p>
<p>实例：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> @<span class="keyword">name</span> = <span class="keyword">name</span> <span class="keyword">from</span> student;</span><br><span class="line"><span class="keyword">select</span> @<span class="keyword">name</span>;</span><br></pre></td></tr></table></figure></p>
<p>第2种：只赋值，不查看结果。语法为：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> 字段列表 <span class="keyword">from</span> 表名 <span class="keyword">into</span> 变量列表;</span><br></pre></td></tr></table></figure></p>
<p>实例：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> <span class="keyword">name</span> <span class="keyword">from</span> student <span class="keyword">where</span> <span class="keyword">id</span> = <span class="number">2</span> <span class="keyword">into</span> @<span class="keyword">name</span>;</span><br><span class="line"><span class="keyword">select</span> @<span class="keyword">name</span>;</span><br><span class="line"><span class="keyword">select</span> * <span class="keyword">from</span> student;</span><br></pre></td></tr></table></figure></p>
<p>注意：自定义变量都是会话级别，只要是当前用户当次连接，都会受到影响，不区分数据库。</p>
<hr>
<h1 id="28、触发器"><a href="#28、触发器" class="headerlink" title="28、触发器"></a>28、触发器</h1><p>触发器：trigger，是指实现为某张表绑定一段代码，当表中的某些内容发生变化（增、删、改）的时候，系统会自动触发代码执行。<br>触发器包含三个要素，分别为：</p>
<ul>
<li>事件类型：增删改，即insert、delete和update</li>
<li>触发时间：事件类型前和后，即before和after</li>
<li>触发对象：表中的每一套记录（行），即整张表</li>
</ul>
<p>每张表只能拥有一种触发时间和一种事件类型的触发器，即每张表最多可以拥有6种触发器。</p>
<h2 id="28-1-创建触发器"><a href="#28-1-创建触发器" class="headerlink" title="28.1 创建触发器"></a>28.1 创建触发器</h2><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">delimiter 自定义符号  </span><br><span class="line"><span class="keyword">create</span> <span class="keyword">trigger</span> 触发器名称 触发器时间 事件类型 <span class="keyword">on</span> 表名 <span class="keyword">for</span> <span class="keyword">each</span> <span class="keyword">row</span>  </span><br><span class="line"><span class="keyword">begin</span>  </span><br><span class="line">触发器内容主体，每行用分号结尾  </span><br><span class="line"><span class="keyword">end</span>  </span><br><span class="line">自定义符号  </span><br><span class="line">delimiter ;</span><br></pre></td></tr></table></figure>
<p>根据上述案例的需求，我们创建两张表，商品表goods和订单表orders，SQL语句如下：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">create</span> <span class="keyword">table</span> goods(</span><br><span class="line">    <span class="keyword">id</span> <span class="built_in">int</span> primary <span class="keyword">key</span> auto_increment,</span><br><span class="line">    <span class="keyword">name</span> <span class="built_in">varchar</span>(<span class="number">20</span>) <span class="keyword">not</span> <span class="literal">null</span>,</span><br><span class="line">    price <span class="built_in">decimal</span>(<span class="number">10</span>, <span class="number">2</span>) <span class="keyword">default</span> <span class="number">0.0</span>,</span><br><span class="line">    inventory <span class="built_in">int</span> <span class="keyword">comment</span> <span class="string">'商品库存量'</span></span><br><span class="line">) <span class="keyword">engine</span> <span class="keyword">InnoDB</span> <span class="keyword">charset</span> utf8;</span><br><span class="line"></span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> goods <span class="keyword">values</span>(<span class="literal">null</span>, <span class="string">'iPhoneX'</span>, <span class="number">7488</span>, <span class="number">1000</span>), (<span class="literal">null</span>, <span class="string">'iPhone8'</span>, <span class="number">5088</span>, <span class="number">1000</span>);</span><br><span class="line"></span><br><span class="line"><span class="keyword">create</span> <span class="keyword">table</span> orders(</span><br><span class="line">    <span class="keyword">id</span> <span class="built_in">int</span> primary <span class="keyword">key</span> auto_increment,</span><br><span class="line">    goods_id <span class="built_in">int</span> <span class="keyword">not</span> <span class="literal">null</span>,</span><br><span class="line">    goods_number <span class="built_in">int</span> <span class="keyword">default</span> <span class="number">1</span></span><br><span class="line">) <span class="keyword">engine</span> <span class="keyword">InnoDB</span> <span class="keyword">charset</span> utf8;</span><br></pre></td></tr></table></figure></p>
<p>创建触发器：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">delimiter $$</span><br><span class="line"><span class="keyword">create</span> <span class="keyword">trigger</span> after_order <span class="keyword">after</span> <span class="keyword">insert</span> <span class="keyword">on</span> orders <span class="keyword">for</span> <span class="keyword">each</span> <span class="keyword">row</span></span><br><span class="line"><span class="keyword">begin</span></span><br><span class="line">    <span class="keyword">update</span> goods <span class="keyword">set</span> inventory = inventory - <span class="number">1</span> <span class="keyword">where</span> <span class="keyword">id</span> = <span class="number">1</span>;</span><br><span class="line"><span class="keyword">end</span></span><br><span class="line">$$</span><br><span class="line">delimiter ;</span><br></pre></td></tr></table></figure></p>
<hr>
<h2 id="28-2-查询触发器"><a href="#28-2-查询触发器" class="headerlink" title="28.2 查询触发器"></a>28.2 查询触发器</h2><p>查询所有触发器或模糊匹配：<br>基本语法：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">show</span> <span class="keyword">triggers</span> [<span class="keyword">like</span> <span class="string">'pattern'</span>]</span><br></pre></td></tr></table></figure></p>
<p>实例：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">show</span> <span class="keyword">triggers</span>\G;</span><br></pre></td></tr></table></figure></p>
<p>\G表示旋转</p>
<p>当然，我们可以查询创建触发器的语句。<br>基本语法：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">show</span> <span class="keyword">create</span> <span class="keyword">trigger</span> 触发器名称;</span><br></pre></td></tr></table></figure></p>
<p>此外，所有的触发器都会被系统保持到information_schema.triggers这张表中，执行如下SQL，进行测试：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> * <span class="keyword">from</span> information_schema.triggers\G;</span><br></pre></td></tr></table></figure></p>
<hr>
<h2 id="28-3-使用触发器"><a href="#28-3-使用触发器" class="headerlink" title="28.3 使用触发器"></a>28.3 使用触发器</h2><p>实际上，触发器不是我们手动触发，而是在某种情况发生的时候自动触发，例如我们上面创建的after_order触发器，当我们insert订单表的时候，该触发器自动执行。执行如下SQL语句，进行测试：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line">mysql&gt; select * from goods;</span><br><span class="line">+<span class="comment">----+---------+---------+-----------+</span></span><br><span class="line">| id | name    | price   | inventory |</span><br><span class="line">+<span class="comment">----+---------+---------+-----------+</span></span><br><span class="line">|  1 | iPhoneX | 7488.00 |      1000 |</span><br><span class="line">|  2 | iPhone8 | 5088.00 |      1000 |</span><br><span class="line">+<span class="comment">----+---------+---------+-----------+</span></span><br><span class="line">2 rows in <span class="keyword">set</span> (<span class="number">0.00</span> sec)</span><br><span class="line"></span><br><span class="line">mysql&gt; <span class="keyword">select</span> * <span class="keyword">from</span> orders;</span><br><span class="line">Empty <span class="keyword">set</span> (<span class="number">0.00</span> sec)</span><br><span class="line"></span><br><span class="line">mysql&gt; <span class="keyword">insert</span> <span class="keyword">into</span> orders <span class="keyword">values</span>(<span class="literal">null</span>, <span class="number">2</span>, <span class="number">10</span>);</span><br><span class="line">Query OK, 1 row affected (0.00 sec)</span><br><span class="line"></span><br><span class="line">mysql&gt; select * from goods;</span><br><span class="line">+<span class="comment">----+---------+---------+-----------+</span></span><br><span class="line">| id | name    | price   | inventory |</span><br><span class="line">+<span class="comment">----+---------+---------+-----------+</span></span><br><span class="line">|  1 | iPhoneX | 7488.00 |       999 |</span><br><span class="line">|  2 | iPhone8 | 5088.00 |      1000 |</span><br><span class="line">+<span class="comment">----+---------+---------+-----------+</span></span><br><span class="line">2 rows in <span class="keyword">set</span> (<span class="number">0.00</span> sec)</span><br><span class="line"></span><br><span class="line">mysql&gt; <span class="keyword">select</span> * <span class="keyword">from</span> orders;</span><br><span class="line">+<span class="comment">----+----------+--------------+</span></span><br><span class="line">| id | goods_id | goods_number |</span><br><span class="line">+<span class="comment">----+----------+--------------+</span></span><br><span class="line">|  1 |        2 |           10 |</span><br><span class="line">+<span class="comment">----+----------+---------</span></span><br></pre></td></tr></table></figure></p>
<p>注意：触发器的触发对象和事件类型，决不能同触发器主体的内容相同，防止发生死循环。</p>
<hr>
<h2 id="28-4-修改触发器-amp-删除触发器"><a href="#28-4-修改触发器-amp-删除触发器" class="headerlink" title="28.4 修改触发器 &amp; 删除触发器"></a>28.4 修改触发器 &amp; 删除触发器</h2><p>触发器不能修改，只能删除。因此，当我们需要修改触发器的时候，唯一的办法就是：先删除，后新增。<br>基本语法：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">drop</span> <span class="keyword">trigger</span> 触发器名称;</span><br></pre></td></tr></table></figure></p>
<p>实例：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">drop</span> <span class="keyword">trigger</span> after_oder;</span><br><span class="line"><span class="keyword">show</span> <span class="keyword">triggers</span>;</span><br></pre></td></tr></table></figure></p>
<hr>
<h2 id="28-5-触发器记录"><a href="#28-5-触发器记录" class="headerlink" title="28.5 触发器记录"></a>28.5 触发器记录</h2><p>触发器记录：无论触发器是否触发，只要当某种操作准备执行，系统就会将当前操作的记录的当前状态和即将执行之后的状态分别记录下来，供触发器使用。其中，当前状态被保存到old中，操作之后的状态被保存到new中。其中old和new保存如下字段：</p>
<ul>
<li>ACTION_REFERENCE_OLD_ROW：OLD</li>
<li>ACTION_REFERENCE_NEW_ROW：NEW</li>
</ul>
<p>其中：  </p>
<ul>
<li>OLD：代表是旧记录，也就是当前记录的状态，插入时没有OLD</li>
<li>NEW：代表新记录，也就是假设操作发生之后记录的状态，删除时没有NEW<br>无论OLD还是 NEW，都代表记录本身，而且任何一条记录除了有数据，还有字段名。因此，使用OLD和 NEW的方法就是：<br>基本语法：  <figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">OLD/NEW + . + 字段名</span><br></pre></td></tr></table></figure>
</li>
</ul>
<p>实例：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">delimiter $$</span><br><span class="line"><span class="keyword">create</span> <span class="keyword">trigger</span> after_order_new <span class="keyword">after</span> <span class="keyword">insert</span> <span class="keyword">on</span> orders <span class="keyword">for</span> <span class="keyword">each</span> <span class="keyword">row</span></span><br><span class="line"><span class="keyword">begin</span></span><br><span class="line">    <span class="keyword">update</span> goods <span class="keyword">set</span> inventory = inventory - NEW.goods_number <span class="keyword">where</span> <span class="keyword">id</span> = NEW.goods_id;</span><br><span class="line"><span class="keyword">end</span></span><br><span class="line">$$</span><br><span class="line">delimiter ;</span><br></pre></td></tr></table></figure></p>
<p>实例：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line">mysql&gt; select * from goods;</span><br><span class="line">+<span class="comment">----+---------+---------+-----------+</span></span><br><span class="line">| id | name    | price   | inventory |</span><br><span class="line">+<span class="comment">----+---------+---------+-----------+</span></span><br><span class="line">|  1 | iPhoneX | 7488.00 |       999 |</span><br><span class="line">|  2 | iPhone8 | 5088.00 |      1000 |</span><br><span class="line">+<span class="comment">----+---------+---------+-----------+</span></span><br><span class="line">2 rows in <span class="keyword">set</span> (<span class="number">0.00</span> sec)</span><br><span class="line"></span><br><span class="line">mysql&gt; <span class="keyword">select</span> * <span class="keyword">from</span> orders;</span><br><span class="line">+<span class="comment">----+----------+--------------+</span></span><br><span class="line">| id | goods_id | goods_number |</span><br><span class="line">+<span class="comment">----+----------+--------------+</span></span><br><span class="line">|  1 |        2 |           10 |</span><br><span class="line">+<span class="comment">----+----------+--------------+</span></span><br><span class="line">1 row in <span class="keyword">set</span> (<span class="number">0.00</span> sec)</span><br><span class="line"></span><br><span class="line">mysql&gt; <span class="keyword">insert</span> <span class="keyword">into</span> orders <span class="keyword">values</span>(<span class="literal">null</span>, <span class="number">2</span>, <span class="number">10</span>);</span><br><span class="line">Query OK, 1 row affected (0.00 sec)</span><br><span class="line"></span><br><span class="line">mysql&gt; select * from goods;</span><br><span class="line">+<span class="comment">----+---------+---------+-----------+</span></span><br><span class="line">| id | name    | price   | inventory |</span><br><span class="line">+<span class="comment">----+---------+---------+-----------+</span></span><br><span class="line">|  1 | iPhoneX | 7488.00 |       999 |</span><br><span class="line">|  2 | iPhone8 | 5088.00 |       990 |</span><br><span class="line">+<span class="comment">----+---------+---------+-----------+</span></span><br><span class="line">2 rows in <span class="keyword">set</span> (<span class="number">0.00</span> sec)</span><br><span class="line"></span><br><span class="line">mysql&gt; <span class="keyword">select</span> * <span class="keyword">from</span> orders;</span><br><span class="line">+<span class="comment">----+----------+--------------+</span></span><br><span class="line">| id | goods_id | goods_number |</span><br><span class="line">+<span class="comment">----+----------+--------------+</span></span><br><span class="line">|  1 |        2 |           10 |</span><br><span class="line">|  3 |        2 |           10 |</span><br><span class="line">+<span class="comment">----+----------+--------------+</span></span><br><span class="line">2 rows in <span class="keyword">set</span> (<span class="number">0.00</span> sec)</span><br></pre></td></tr></table></figure></p>
<hr>
<h1 id="29、代码执行结构"><a href="#29、代码执行结构" class="headerlink" title="29、代码执行结构"></a>29、代码执行结构</h1><p>在MySQL编程中，代码的执行结构有三种，分别为：  </p>
<ul>
<li>顺序结构</li>
<li>分支结构</li>
<li>循环结构</li>
</ul>
<p>下面主要说分支结构和循环结构。</p>
<hr>
<h2 id="29-1-分支结构"><a href="#29-1-分支结构" class="headerlink" title="29.1 分支结构"></a>29.1 分支结构</h2><p>分支结构：事先准备多个代码代码块，通过判断条件是否满足，执行对应的代码。<br>在MySQL中，只有if分支结构，其基本语法为：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">if 判断条件 then</span><br><span class="line">    <span class="comment">-- 满足条件时，要执行的代码</span></span><br><span class="line">esle</span><br><span class="line">    <span class="comment">-- 不满足条件时，要执行的代码</span></span><br><span class="line"><span class="keyword">end</span> <span class="keyword">if</span>;</span><br></pre></td></tr></table></figure></p>
<p>接下来，我们利用触发器和if分支，完成这样的需求：<br>在生成订单前，判断商品的库存是否满足，如果满足，则插入订单；否则插入失败。<br>实例：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">delimiter $$</span><br><span class="line"><span class="keyword">create</span> <span class="keyword">trigger</span> before_order <span class="keyword">before</span> <span class="keyword">insert</span> <span class="keyword">on</span> orders <span class="keyword">for</span> <span class="keyword">each</span> <span class="keyword">row</span></span><br><span class="line"><span class="keyword">begin</span></span><br><span class="line">    <span class="keyword">select</span> inventory <span class="keyword">from</span> goods <span class="keyword">where</span> <span class="keyword">id</span> = NEW.goods_id <span class="keyword">into</span> @inventory;</span><br><span class="line">    if @inventory &lt; NEW.goods_number then</span><br><span class="line">        <span class="keyword">insert</span> <span class="keyword">into</span> xxx <span class="keyword">values</span>(xxx);</span><br><span class="line">    <span class="keyword">end</span> <span class="keyword">if</span>;</span><br><span class="line"><span class="keyword">end</span></span><br><span class="line">$$</span><br><span class="line">delimiter ;</span><br></pre></td></tr></table></figure></p>
<hr>
<h2 id="29-2-循环结构"><a href="#29-2-循环结构" class="headerlink" title="29.2 循环结构"></a>29.2 循环结构</h2><p>在MySQL中，循环有while循环、loop循环和repeat循环，还有非标准的goto循环，这里介绍while循环，其基本语法为：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">while 条件判断 <span class="keyword">do</span></span><br><span class="line">    <span class="comment">-- 满足条件要执行的代码</span></span><br><span class="line"><span class="keyword">end</span> <span class="keyword">while</span>;</span><br></pre></td></tr></table></figure></p>
<p>在使用循环结构的时候，我们经常需要对循环进行控制，即在循环结构内部进行判断和控制。虽然在 MySQL 中没有continue和break，但是有其替代关键字：</p>
<ul>
<li>iterate：迭代，类似于continue，表示结束本次循环，不执行后续步骤，直接开始下一次循环</li>
<li>leave：离开，类似于break，直接结束整个循环</li>
</ul>
<p>上述两个关键字的使用方法为：<br>基本语法：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">iterate/leave 循环名称;</span><br></pre></td></tr></table></figure></p>
<hr>
<h1 id="30、函数"><a href="#30、函数" class="headerlink" title="30、函数"></a>30、函数</h1><p>函数，就是将一段代码封装到一个结构中，在需要执行该代码的时候，直接调用该结构（函数）执行即可。此操作，实现了代码的复用。在MySQL中，函数有两种，分别为：系统函数和自定义函数。</p>
<h2 id="30-1-系统函数"><a href="#30-1-系统函数" class="headerlink" title="30.1 系统函数"></a>30.1 系统函数</h2><p>任何函数都有返回值（对于空函数，我们认为其返回值为空），而且在MySQL中任何返回值的操作都是通过select来操作的，因此MySQL的函数调用就是通过select来实现的。<br>下面，我们介绍一些常见的、对字符进行操作的系统函数：首先，执行如下语句，定义一些变量：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">set</span> @cn = <span class="string">'你好世界'</span>;</span><br><span class="line"><span class="keyword">set</span> @en = <span class="string">'hello world'</span>;</span><br><span class="line"><span class="keyword">set</span> @one = <span class="string">'harlon'</span>;</span><br><span class="line"><span class="keyword">set</span> @two = <span class="string">'jack'</span>;</span><br><span class="line"><span class="keyword">set</span> @three = <span class="string">'jack'</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">select</span> @cn, @en, @one, @two, @three;</span><br></pre></td></tr></table></figure></p>
<p>字符串函数：</p>
<ul>
<li>substring，截取字符串，单位为字符。</li>
</ul>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">mysql&gt; select substring(@cn, 1, 1), substring(@en, 1, 2);</span><br><span class="line">+<span class="comment">----------------------+----------------------+</span></span><br><span class="line">| substring(@cn, 1, 1) | substring(@en, 1, 2) |</span><br><span class="line">+<span class="comment">----------------------+----------------------+</span></span><br><span class="line">| 你                   | he                   |</span><br><span class="line">+<span class="comment">----------------------+----------------------+</span></span><br><span class="line">1 row in <span class="keyword">set</span> (<span class="number">0.00</span> sec)</span><br></pre></td></tr></table></figure>
<ul>
<li>instr，判断字符串中某个子串是否存在，若存在返回具体的位置，不存在返回0。</li>
</ul>
<p>mysql&gt; select instr(@cn, ‘你好’), instr(@cn, ‘世’), instr(@en, ‘c’);<br>+———————-+——————-+—————–+<br>| instr(@cn, ‘你好’)   | instr(@cn, ‘世’)  | instr(@en, ‘c’) |<br>+———————-+——————-+—————–+<br>|                    1 |                 3 |               0 |<br>+———————-+——————-+—————–+<br>1 row in set (0.00 sec)</p>
<ul>
<li>lpad：左填充，将字符串按照某个指定的填充方式，填充到指定长度。</li>
</ul>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">mysql&gt; select lpad(@cn, 20, '曾经'), lpad(@cn, 20, 'hello');</span><br><span class="line">+<span class="comment">--------------------------------------------------------------+------------------------------+</span></span><br><span class="line">| lpad(@cn, 20, '曾经')                                        | lpad(@cn, 20, 'hello')       |</span><br><span class="line">+<span class="comment">--------------------------------------------------------------+------------------------------+</span></span><br><span class="line">| 曾经曾经曾经曾经曾经曾经曾经曾经你好世界                     | hellohellohelloh你好世界     |</span><br><span class="line">+<span class="comment">--------------------------------------------------------------+------------------------------+</span></span><br><span class="line">1 row in <span class="keyword">set</span> (<span class="number">0.00</span> sec)</span><br></pre></td></tr></table></figure>
<ul>
<li>insert，找到目标位置，将指定长度的字符串替换为目标字符串。</li>
</ul>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">mysql&gt; select insert(@en, 2, 4, 'i'), @en;</span><br><span class="line">+<span class="comment">------------------------+-------------+</span></span><br><span class="line">| <span class="keyword">insert</span>(@en, <span class="number">2</span>, <span class="number">4</span>, <span class="string">'i'</span>) | @en         |</span><br><span class="line">+<span class="comment">------------------------+-------------+</span></span><br><span class="line">| hi world               | hello world |</span><br><span class="line">+<span class="comment">------------------------+-------------+</span></span><br><span class="line"><span class="number">1</span> <span class="keyword">row</span> <span class="keyword">in</span> <span class="keyword">set</span> (<span class="number">0.00</span> sec)</span><br></pre></td></tr></table></figure>
<ul>
<li>strcmp，比较字符串的大小。</li>
</ul>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">mysql&gt; select strcmp(@one, @two), strcmp(@two, @three), strcmp(@three, @one);</span><br><span class="line">+<span class="comment">--------------------+----------------------+----------------------+</span></span><br><span class="line">| strcmp(@one, @two) | strcmp(@two, @three) | strcmp(@three, @one) |</span><br><span class="line">+<span class="comment">--------------------+----------------------+----------------------+</span></span><br><span class="line">|                 -1 |                    0 |                    1 |</span><br><span class="line">+<span class="comment">--------------------+-----------</span></span><br></pre></td></tr></table></figure>
<hr>
<h2 id="30-2-自定义函数"><a href="#30-2-自定义函数" class="headerlink" title="30.2 自定义函数"></a>30.2 自定义函数</h2><p>对于任意一个函数，都包含如下要素：</p>
<ul>
<li>函数名</li>
<li>参数列表</li>
<li>返回值</li>
<li>函数体</li>
</ul>
<hr>
<h2 id="30-3-创建函数"><a href="#30-3-创建函数" class="headerlink" title="30.3 创建函数"></a>30.3 创建函数</h2><p>基本语法：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">create</span> <span class="keyword">function</span> 函数名([参数列表]) <span class="keyword">returns</span> 数据类型</span><br><span class="line"><span class="keyword">begin</span></span><br><span class="line">    <span class="comment">-- 函数体</span></span><br><span class="line"><span class="keyword">end</span></span><br></pre></td></tr></table></figure></p>
<p>实例：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">create</span> <span class="keyword">function</span> showLove() <span class="keyword">returns</span> <span class="built_in">int</span></span><br><span class="line"><span class="keyword">return</span> <span class="number">521</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">select</span> showLove();</span><br></pre></td></tr></table></figure></p>
<hr>
<h2 id="30-4-查看函数"><a href="#30-4-查看函数" class="headerlink" title="30.4 查看函数"></a>30.4 查看函数</h2><p>查看函数，基本语法为：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">show</span> <span class="keyword">function</span> <span class="keyword">status</span> + [<span class="keyword">like</span> <span class="string">'pattern'</span>]</span><br></pre></td></tr></table></figure></p>
<p>查看函数的创建语句，基本语法为：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">show</span> <span class="keyword">create</span> <span class="keyword">function</span> 函数名;</span><br></pre></td></tr></table></figure></p>
<hr>
<h2 id="30-5-修改函数-amp-删除函数"><a href="#30-5-修改函数-amp-删除函数" class="headerlink" title="30.5 修改函数 &amp; 删除函数"></a>30.5 修改函数 &amp; 删除函数</h2><p>函数只能先删除后新增，不能修改，删除函数的基本语法为：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">drop</span> <span class="keyword">function</span> 函数名;</span><br></pre></td></tr></table></figure></p>
<hr>
<h2 id="30-6-函数参数"><a href="#30-6-函数参数" class="headerlink" title="30.6 函数参数"></a>30.6 函数参数</h2><p>对于函数的参数，一共有两种，分别为形参和实参，其中，形参可以理解为定义函数时使用的参数，且形参必须指定数据类型；实参可以理解为在调用函数时传入的值或变量。因此，函数定义的具体形式应该为：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">function 函数名(形参名字 形参类型) returns 返回数据类型</span><br></pre></td></tr></table></figure></p>
<p>定义一个函数，求1到指定数值的和。<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line">delimiter $$</span><br><span class="line"><span class="keyword">create</span> <span class="keyword">function</span> addAll(<span class="keyword">num</span> <span class="built_in">int</span>) <span class="keyword">returns</span> <span class="built_in">int</span></span><br><span class="line"><span class="keyword">begin</span></span><br><span class="line">    <span class="keyword">set</span> @i = <span class="number">1</span>;</span><br><span class="line">    <span class="keyword">set</span> @res = <span class="number">0</span>;</span><br><span class="line">    while @i &lt; num do</span><br><span class="line">        <span class="keyword">set</span> @res = @res + @i;</span><br><span class="line">        <span class="keyword">set</span> @i = @i +<span class="number">1</span>;</span><br><span class="line">    <span class="keyword">end</span> <span class="keyword">while</span>;</span><br><span class="line">    return @res;</span><br><span class="line"><span class="keyword">end</span></span><br><span class="line">$$</span><br><span class="line">delimiter ;</span><br><span class="line"></span><br><span class="line">mysql&gt; select addAll(100);</span><br><span class="line">+<span class="comment">-------------+</span></span><br><span class="line">| addAll(100) |</span><br><span class="line">+<span class="comment">-------------+</span></span><br><span class="line">|        4950 |</span><br><span class="line">+<span class="comment">-------------+</span></span><br><span class="line">1 row in <span class="keyword">set</span> (<span class="number">0.01</span> sec)</span><br><span class="line"></span><br><span class="line">mysql&gt; <span class="keyword">select</span> @i, @res;</span><br><span class="line">+<span class="comment">------+------+</span></span><br><span class="line">| @i   | @res |</span><br><span class="line">+<span class="comment">------+------+</span></span><br><span class="line">|  100 | 4950 |</span><br><span class="line">+<span class="comment">------+------+</span></span><br><span class="line">1 row in <span class="keyword">set</span> (<span class="number">0.00</span> sec)</span><br></pre></td></tr></table></figure></p>
<hr>
<h2 id="30-7-变量作用域"><a href="#30-7-变量作用域" class="headerlink" title="30.7 变量作用域"></a>30.7 变量作用域</h2><p>在 MySQL 中，变量的作用域有两种，分别为全局和局部，其中，全局变量可以在任何地方使用；局部变量只能在函数内部使用。</p>
<ul>
<li>全局变量：使用set关键字定义，用@符号标识</li>
<li>局部变量：使用declare关键字声明，且所用的局部变量必须在函数体开始之前进行声明</li>
</ul>
<p>定义一个函数，即求1到指定数值的和，要求10的倍数不加，代码如下：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line">delimiter $$</span><br><span class="line"><span class="keyword">create</span> <span class="keyword">function</span> addAll2(<span class="keyword">num</span> <span class="built_in">int</span>) <span class="keyword">returns</span> <span class="built_in">int</span></span><br><span class="line"><span class="keyword">begin</span></span><br><span class="line">    <span class="keyword">declare</span> i <span class="built_in">int</span> <span class="keyword">default</span> <span class="number">1</span>;</span><br><span class="line">    <span class="keyword">declare</span> res <span class="built_in">int</span> <span class="keyword">default</span> <span class="number">0</span>;</span><br><span class="line">    mywhile:while @i &lt; num do</span><br><span class="line">        if i % 10 = 0 then</span><br><span class="line">            <span class="keyword">set</span> i = i + <span class="number">1</span>;</span><br><span class="line">            iterate mywhile;</span><br><span class="line">    <span class="keyword">end</span> <span class="keyword">if</span>;</span><br><span class="line">    <span class="keyword">set</span> res = res + i;</span><br><span class="line">    <span class="keyword">set</span> i = i + <span class="number">1</span>;</span><br><span class="line">    <span class="keyword">end</span> <span class="keyword">while</span>;</span><br><span class="line">    return @res;</span><br><span class="line"><span class="keyword">end</span></span><br><span class="line">$$</span><br><span class="line">delimiter ;</span><br><span class="line"></span><br><span class="line">mysql&gt; select addAll(100), addAll(200);</span><br><span class="line">+<span class="comment">-------------+-------------+</span></span><br><span class="line">| addAll(100) | addAll(200) |</span><br><span class="line">+<span class="comment">-------------+-------------+</span></span><br><span class="line">|        4950 |       19900 |</span><br><span class="line">+<span class="comment">-------------+-------------+</span></span><br><span class="line">1 row in <span class="keyword">set</span> (<span class="number">0.00</span> sec)</span><br></pre></td></tr></table></figure></p>
<hr>
<h1 id="31、存储过程"><a href="#31、存储过程" class="headerlink" title="31、存储过程"></a>31、存储过程</h1><p>存储过程：produce，是一种用来处理数据（增删改）的方式，简单点，我们也可以将其理解为没有返回值的函数。</p>
<h2 id="31-1-创建过程"><a href="#31-1-创建过程" class="headerlink" title="31.1 创建过程"></a>31.1 创建过程</h2><p>基本语法：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">create</span> proceduce 过程名([参数列表])</span><br><span class="line"><span class="keyword">begin</span></span><br><span class="line">    <span class="comment">-- 过程体</span></span><br><span class="line"><span class="keyword">end</span></span><br></pre></td></tr></table></figure></p>
<p>实例：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">create</span> <span class="keyword">procedure</span> pro()</span><br><span class="line"><span class="keyword">select</span> * <span class="keyword">from</span> student;</span><br></pre></td></tr></table></figure></p>
<hr>
<h2 id="32-2-查看过程"><a href="#32-2-查看过程" class="headerlink" title="32.2 查看过程"></a>32.2 查看过程</h2><p>查看过程，基本语法为：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">show</span> <span class="keyword">procedure</span> <span class="keyword">status</span> [<span class="keyword">like</span> <span class="string">'pattern'</span>];</span><br></pre></td></tr></table></figure></p>
<p>查看过程的创建语句，基本语法为：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">show</span> <span class="keyword">create</span> <span class="keyword">procedure</span> 过程名;</span><br></pre></td></tr></table></figure></p>
<hr>
<h2 id="32-3-调用过程"><a href="#32-3-调用过程" class="headerlink" title="32.3 调用过程"></a>32.3 调用过程</h2><p>基本语法：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">call</span> 过程名;</span><br></pre></td></tr></table></figure></p>
<hr>
<h2 id="33-3-修改过程-amp-删除过程"><a href="#33-3-修改过程-amp-删除过程" class="headerlink" title="33.3 修改过程 &amp; 删除过程"></a>33.3 修改过程 &amp; 删除过程</h2><p>过程只能先删除后新增，不能修改。删除过程的基本语法为：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">drop</span> <span class="keyword">procedure</span> 过程名;</span><br></pre></td></tr></table></figure></p>
<hr>
<h2 id="33-4-过程参数"><a href="#33-4-过程参数" class="headerlink" title="33.4 过程参数"></a>33.4 过程参数</h2><p>函数的参数需要指定数据类型，过程比函数更加严格。过程有三种自己的参数类型，分别为：</p>
<ul>
<li>in：数据只是从过程外部传入给过程内部使用，可以使数值也可以使变量</li>
<li>out：参数只能传递变量，且变量指向的数据需要先清空然后才能进入过程内部，该引用供过程内部使用，过程结束后可以将变量的值传递给过程外部使用</li>
<li>inout：此参数只能传递变量，该变量的值可以给过程内部使用，过程结束后可以变量的值传递给过程外部使用</li>
</ul>
<p>因此，过程定义的具体形式应该为：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">procedure 过程名(in 参数名字 参数类型, out 参数名字 参数类型, inout 参数名字 参数类型)</span><br></pre></td></tr></table></figure></p>
<p>实例：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">delimiter $$</span><br><span class="line"><span class="keyword">create</span> <span class="keyword">procedure</span> pro2(<span class="keyword">in</span> var1 <span class="built_in">int</span>, <span class="keyword">out</span> var2 <span class="built_in">int</span>, inout var3 <span class="built_in">int</span>)</span><br><span class="line"><span class="keyword">begin</span></span><br><span class="line">    <span class="keyword">select</span> var1, var2, var3;</span><br><span class="line"><span class="keyword">end</span></span><br><span class="line">$$</span><br><span class="line">delimiter ;</span><br></pre></td></tr></table></figure></p>
<p>测试：<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">set</span> @var1 = <span class="number">1</span>;</span><br><span class="line"><span class="keyword">set</span> @var2 = <span class="number">2</span>;</span><br><span class="line"><span class="keyword">set</span> @var3 = <span class="number">3</span>;</span><br><span class="line">mysql&gt; call pro2(@var1, @var2, @var3);</span><br><span class="line">+<span class="comment">------+------+------+</span></span><br><span class="line">| var1 | var2 | var3 |</span><br><span class="line">+<span class="comment">------+------+------+</span></span><br><span class="line">|    1 | NULL |    3 |</span><br><span class="line">+<span class="comment">------+------+------+</span></span><br><span class="line">1 row in <span class="keyword">set</span> (<span class="number">0.00</span> sec)</span><br><span class="line"></span><br><span class="line"><span class="keyword">Query</span> OK, <span class="number">0</span> <span class="keyword">rows</span> affected (<span class="number">0.00</span> sec)</span><br><span class="line"></span><br><span class="line">mysql&gt; <span class="keyword">select</span> @var1, @var2, @var3;</span><br><span class="line">+<span class="comment">-------+-------+-------+</span></span><br><span class="line">| @var1 | @var2 | @var3 |</span><br><span class="line">+<span class="comment">-------+-------+-------+</span></span><br><span class="line">|     1 |  NULL |     3 |   </span><br><span class="line">+<span class="comment">-------+-------+-------+</span></span><br><span class="line">1 row in <span class="keyword">set</span> (<span class="number">0.00</span> sec)</span><br></pre></td></tr></table></figure></p>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2019/07/17/kernel-5/" rel="next" title="Linux系统之链表">
                <i class="fa fa-chevron-left"></i> Linux系统之链表
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            Table of Contents
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            Overview
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <p class="site-author-name" itemprop="name">LuQiLin</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives">
              
                  <span class="site-state-item-count">80</span>
                  <span class="site-state-item-name">posts</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                
                  <span class="site-state-item-count">1</span>
                  <span class="site-state-item-name">categories</span>
                
              </div>
            

            

          </nav>

          

          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#1、SQL语言主要分为三类"><span class="nav-number">1.</span> <span class="nav-text">1、SQL语言主要分为三类</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#2、库操作"><span class="nav-number">2.</span> <span class="nav-text">2、库操作</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#2-1-新增数据库"><span class="nav-number">2.1.</span> <span class="nav-text">2.1 新增数据库</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-2-查询数据库"><span class="nav-number">2.2.</span> <span class="nav-text">2.2 查询数据库</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-3-更新数据库"><span class="nav-number">2.3.</span> <span class="nav-text">2.3 更新数据库</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-4-删除数据库"><span class="nav-number">2.4.</span> <span class="nav-text">2.4 删除数据库</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#3、表操作"><span class="nav-number">3.</span> <span class="nav-text">3、表操作</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#3-1-新增表"><span class="nav-number">3.1.</span> <span class="nav-text">3.1 新增表</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-2-查询表"><span class="nav-number">3.2.</span> <span class="nav-text">3.2 查询表</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-3-更新表"><span class="nav-number">3.3.</span> <span class="nav-text">3.3 更新表</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-4-删除表"><span class="nav-number">3.4.</span> <span class="nav-text">3.4 删除表</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#4、数据操作"><span class="nav-number">4.</span> <span class="nav-text">4、数据操作</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#4-1-新增数据"><span class="nav-number">4.1.</span> <span class="nav-text">4.1 新增数据</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-2-查询数据"><span class="nav-number">4.2.</span> <span class="nav-text">4.2 查询数据</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-3-更新数据"><span class="nav-number">4.3.</span> <span class="nav-text">4.3 更新数据</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-3-删除数据"><span class="nav-number">4.4.</span> <span class="nav-text">4.3 删除数据</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#5、字符集问题"><span class="nav-number">5.</span> <span class="nav-text">5、字符集问题</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#6、校对集问题"><span class="nav-number">6.</span> <span class="nav-text">6、校对集问题</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#7、数值型"><span class="nav-number">7.</span> <span class="nav-text">7、数值型</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#7-1-整数型"><span class="nav-number">7.1.</span> <span class="nav-text">7.1 整数型</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#7-2-小数型"><span class="nav-number">7.2.</span> <span class="nav-text">7.2 小数型</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#8、日期时间类型"><span class="nav-number">8.</span> <span class="nav-text">8、日期时间类型</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#9、字符类型"><span class="nav-number">9.</span> <span class="nav-text">9、字符类型</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#10、记录长度"><span class="nav-number">10.</span> <span class="nav-text">10、记录长度</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#11、列属性"><span class="nav-number">11.</span> <span class="nav-text">11、列属性</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#11-1-空属性"><span class="nav-number">11.1.</span> <span class="nav-text">11.1 空属性</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#11-2-列描述"><span class="nav-number">11.2.</span> <span class="nav-text">11.2 列描述</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#11-3-默认值"><span class="nav-number">11.3.</span> <span class="nav-text">11.3 默认值</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#11-4-主键"><span class="nav-number">11.4.</span> <span class="nav-text">11.4 主键</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#11-5-自动增长"><span class="nav-number">11.5.</span> <span class="nav-text">11.5 自动增长</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#11-6-唯一键"><span class="nav-number">11.6.</span> <span class="nav-text">11.6 唯一键</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#12、索引"><span class="nav-number">12.</span> <span class="nav-text">12、索引</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#13、关系"><span class="nav-number">13.</span> <span class="nav-text">13、关系</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#13-1-一对一"><span class="nav-number">13.1.</span> <span class="nav-text">13.1 一对一</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#13-2-一对多"><span class="nav-number">13.2.</span> <span class="nav-text">13.2  一对多</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#13-3-多对多"><span class="nav-number">13.3.</span> <span class="nav-text">13.3 多对多</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#14、范式"><span class="nav-number">14.</span> <span class="nav-text">14、范式</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#14-1-1NF"><span class="nav-number">14.1.</span> <span class="nav-text">14.1 1NF</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#14-2-2NF"><span class="nav-number">14.2.</span> <span class="nav-text">14.2 2NF</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#14-3-3NF"><span class="nav-number">14.3.</span> <span class="nav-text">14.3 3NF</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#14-4-逆规范化"><span class="nav-number">14.4.</span> <span class="nav-text">14.4 逆规范化</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#15、主键冲突"><span class="nav-number">15.</span> <span class="nav-text">15、主键冲突</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#16、蠕虫复制"><span class="nav-number">16.</span> <span class="nav-text">16、蠕虫复制</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#17、数据库高级操作"><span class="nav-number">17.</span> <span class="nav-text">17、数据库高级操作</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#17-1-更新数据"><span class="nav-number">17.1.</span> <span class="nav-text">17.1 更新数据</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#17-2-删除数据"><span class="nav-number">17.2.</span> <span class="nav-text">17.2 删除数据</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#18、查询高级操作"><span class="nav-number">18.</span> <span class="nav-text">18、查询高级操作</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#18-1-select-选项"><span class="nav-number">18.1.</span> <span class="nav-text">18.1 select 选项</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#18-2-字段别名"><span class="nav-number">18.2.</span> <span class="nav-text">18.2 字段别名</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#18-3-数据源"><span class="nav-number">18.3.</span> <span class="nav-text">18.3 数据源</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#18-4-where子句"><span class="nav-number">18.4.</span> <span class="nav-text">18.4 where子句</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#18-5-group-by-子句"><span class="nav-number">18.5.</span> <span class="nav-text">18.5 group by 子句</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#18-6-having-子句"><span class="nav-number">18.6.</span> <span class="nav-text">18.6 having 子句</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#18-7-order-by子句"><span class="nav-number">18.7.</span> <span class="nav-text">18.7 order by子句</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#19、连接查询"><span class="nav-number">19.</span> <span class="nav-text">19、连接查询</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#19-1-交叉连接"><span class="nav-number">19.1.</span> <span class="nav-text">19.1 交叉连接</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#19-2-内连接"><span class="nav-number">19.2.</span> <span class="nav-text">19.2 内连接</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#19-3-外链接"><span class="nav-number">19.3.</span> <span class="nav-text">19.3 外链接</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#19-4-自然连接"><span class="nav-number">19.4.</span> <span class="nav-text">19.4 自然连接</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#20、外键"><span class="nav-number">20.</span> <span class="nav-text">20、外键</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#20-1-外键操作"><span class="nav-number">20.1.</span> <span class="nav-text">20.1 外键操作</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#20-2-外键作用"><span class="nav-number">20.2.</span> <span class="nav-text">20.2 外键作用</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#20-3-外键条件"><span class="nav-number">20.3.</span> <span class="nav-text">20.3 外键条件</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#20-4-外键约束"><span class="nav-number">20.4.</span> <span class="nav-text">20.4 外键约束</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#21、联合查询"><span class="nav-number">21.</span> <span class="nav-text">21、联合查询</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#22、子查询"><span class="nav-number">22.</span> <span class="nav-text">22、子查询</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#22-1-标量子查询"><span class="nav-number">22.1.</span> <span class="nav-text">22.1 标量子查询</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#22-2-列子查询"><span class="nav-number">22.2.</span> <span class="nav-text">22.2 列子查询</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#22-3-行子查询"><span class="nav-number">22.3.</span> <span class="nav-text">22.3 行子查询</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#22-4-表子查询"><span class="nav-number">22.4.</span> <span class="nav-text">22.4 表子查询</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#22-5-exists子查询"><span class="nav-number">22.5.</span> <span class="nav-text">22.5 exists子查询</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#23、视图"><span class="nav-number">23.</span> <span class="nav-text">23、视图</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#23-1-创建视图"><span class="nav-number">23.1.</span> <span class="nav-text">23.1 创建视图</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#23-2-查询视图"><span class="nav-number">23.2.</span> <span class="nav-text">23.2 查询视图</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#23-3-使用视图"><span class="nav-number">23.3.</span> <span class="nav-text">23.3 使用视图</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#23-4-修改视图"><span class="nav-number">23.4.</span> <span class="nav-text">23.4 修改视图</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#23-5-删除视图"><span class="nav-number">23.5.</span> <span class="nav-text">23.5 删除视图</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#23-6-视图意义"><span class="nav-number">23.6.</span> <span class="nav-text">23.6 视图意义</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#24、视图数据操作"><span class="nav-number">24.</span> <span class="nav-text">24、视图数据操作</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#24-1-新增数据"><span class="nav-number">24.1.</span> <span class="nav-text">24.1 新增数据</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#24-2-删除数据"><span class="nav-number">24.2.</span> <span class="nav-text">24.2 删除数据</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#24-3-更新数据"><span class="nav-number">24.3.</span> <span class="nav-text">24.3 更新数据</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#24-4-视图算法"><span class="nav-number">24.4.</span> <span class="nav-text">24.4 视图算法</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#25、数据备份与还原"><span class="nav-number">25.</span> <span class="nav-text">25、数据备份与还原</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#25-1-数据表备份"><span class="nav-number">25.1.</span> <span class="nav-text">25.1 数据表备份</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#25-2-单数据表备份"><span class="nav-number">25.2.</span> <span class="nav-text">25.2 单数据表备份</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#25-3-SQL备份"><span class="nav-number">25.3.</span> <span class="nav-text">25.3 SQL备份</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#25-4-增量备份"><span class="nav-number">25.4.</span> <span class="nav-text">25.4 增量备份</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#26、事务"><span class="nav-number">26.</span> <span class="nav-text">26、事务</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#26-1-事务操作"><span class="nav-number">26.1.</span> <span class="nav-text">26.1 事务操作</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#26-2-事务原理"><span class="nav-number">26.2.</span> <span class="nav-text">26.2 事务原理</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#26-3-回滚点"><span class="nav-number">26.3.</span> <span class="nav-text">26.3 回滚点</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#26-4-自动事务"><span class="nav-number">26.4.</span> <span class="nav-text">26.4 自动事务</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#26-5-事务特性"><span class="nav-number">26.5.</span> <span class="nav-text">26.5 事务特性</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#27、数据库变量"><span class="nav-number">27.</span> <span class="nav-text">27、数据库变量</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#27-1-系统变量"><span class="nav-number">27.1.</span> <span class="nav-text">27.1 系统变量</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#27-2-自定义变量"><span class="nav-number">27.2.</span> <span class="nav-text">27.2 自定义变量</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#28、触发器"><span class="nav-number">28.</span> <span class="nav-text">28、触发器</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#28-1-创建触发器"><span class="nav-number">28.1.</span> <span class="nav-text">28.1 创建触发器</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#28-2-查询触发器"><span class="nav-number">28.2.</span> <span class="nav-text">28.2 查询触发器</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#28-3-使用触发器"><span class="nav-number">28.3.</span> <span class="nav-text">28.3 使用触发器</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#28-4-修改触发器-amp-删除触发器"><span class="nav-number">28.4.</span> <span class="nav-text">28.4 修改触发器 &amp; 删除触发器</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#28-5-触发器记录"><span class="nav-number">28.5.</span> <span class="nav-text">28.5 触发器记录</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#29、代码执行结构"><span class="nav-number">29.</span> <span class="nav-text">29、代码执行结构</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#29-1-分支结构"><span class="nav-number">29.1.</span> <span class="nav-text">29.1 分支结构</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#29-2-循环结构"><span class="nav-number">29.2.</span> <span class="nav-text">29.2 循环结构</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#30、函数"><span class="nav-number">30.</span> <span class="nav-text">30、函数</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#30-1-系统函数"><span class="nav-number">30.1.</span> <span class="nav-text">30.1 系统函数</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#30-2-自定义函数"><span class="nav-number">30.2.</span> <span class="nav-text">30.2 自定义函数</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#30-3-创建函数"><span class="nav-number">30.3.</span> <span class="nav-text">30.3 创建函数</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#30-4-查看函数"><span class="nav-number">30.4.</span> <span class="nav-text">30.4 查看函数</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#30-5-修改函数-amp-删除函数"><span class="nav-number">30.5.</span> <span class="nav-text">30.5 修改函数 &amp; 删除函数</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#30-6-函数参数"><span class="nav-number">30.6.</span> <span class="nav-text">30.6 函数参数</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#30-7-变量作用域"><span class="nav-number">30.7.</span> <span class="nav-text">30.7 变量作用域</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#31、存储过程"><span class="nav-number">31.</span> <span class="nav-text">31、存储过程</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#31-1-创建过程"><span class="nav-number">31.1.</span> <span class="nav-text">31.1 创建过程</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#32-2-查看过程"><span class="nav-number">31.2.</span> <span class="nav-text">32.2 查看过程</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#32-3-调用过程"><span class="nav-number">31.3.</span> <span class="nav-text">32.3 调用过程</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#33-3-修改过程-amp-删除过程"><span class="nav-number">31.4.</span> <span class="nav-text">33.3 修改过程 &amp; 删除过程</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#33-4-过程参数"><span class="nav-number">31.5.</span> <span class="nav-text">33.4 过程参数</span></a></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2019</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">LuQiLin</span>

  
</div>


  <div class="powered-by">Powered by <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a></div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">Theme &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Mist</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  

  

  

</body>
</html>
